Delete commits from a branch in Git

后端 未结 30 2003
醉话见心
醉话见心 2020-11-21 07:17

I would like to know how to delete a commit.

By delete, I mean it is as if I didn\'t make that commit, and when I do a push in the future, my changes wi

相关标签:
30条回答
  • 2020-11-21 07:44

    Another possibility is one of my personal favorite commands:

    git rebase -i <commit>~1
    

    This will start the rebase in interactive mode -i at the point just before the commit you want to whack. The editor will start up listing all of the commits since then. Delete the line containing the commit you want to obliterate and save the file. Rebase will do the rest of the work, deleting only that commit, and replaying all of the others back into the log.

    0 讨论(0)
  • 2020-11-21 07:45

    I have already pushed. Need to return some commits back remotly. Have tried many variations, but only this from Justin via git bush is working fine for me:

    git reset --hard $GIT_COMMIT_HASH_HERE
    git push origin HEAD --force
    
    0 讨论(0)
  • 2020-11-21 07:45

    git reset --hard HEAD~1
    You will be now at previous head. Pull the branch. Push new code. Commit will be removed from git

    0 讨论(0)
  • 2020-11-21 07:46

    If you didn't publish changes, to remove latest commit, you can do

    $ git reset --hard HEAD^
    

    (note that this would also remove all uncommitted changes; use with care).

    If you already published to-be-deleted commit, use git revert

    $ git revert HEAD
    
    0 讨论(0)
  • 2020-11-21 07:46

    [Quick Answer]

    You have many alternatives, for example:

    • Alternative 1:

      git rebase -i <YourCommitId>~1
      

      Change YourCommitId for the number of the commit which you want to revert back to.

    • Alternative 2:

      git reset --hard YourCommitId
      git push <origin> <branch> --force
      

      Change YourCommitId for the number of the commit which you want to revert back to.

      I don't recommend this option because you can lost your work in progress.

    • Alternative 3:

      git reset --soft HEAD~1
      

      You can keep your work and only undo the commit.

    0 讨论(0)
  • 2020-11-21 07:49

    Assuming you have not pushed to the remote repository, you could re-clone the repository. This has been my method of choice a few times.

    0 讨论(0)
提交回复
热议问题