Delete commits from a branch in Git

后端 未结 30 1764
醉话见心
醉话见心 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:37
    git rebase -i HEAD~2
    

    Here '2' is the number of commits you want to rebase.

    'git rebase -i HEAD`
    

    if you want to rebase all the commits.

    Then you will be able to choose one of these options.

    p, pick = use commit

    r, reword = use commit, but edit the commit message

    e, edit = use commit, but stop for amending

    s, squash = use commit, but meld into previous commit

    f, fixup = like "squash", but discard this commit's log message

    x, exec = run command (the rest of the line) using shell

    d, drop = remove commit

    These lines can be re-ordered; they are executed from top to bottom. If you remove a line here THAT COMMIT WILL BE LOST. However, if you remove everything, the rebase will be aborted. Note that empty commits are commented out

    You can simply remove that commit using option "d" or Removing a line that has your commit.

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

    use git revert https://git-scm.com/docs/git-revert .It will revert all code then you can do next commit.Then head will point to that last commit. reverted commits never delete but it will not affect on you last commit.

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

    Take backup of your code in to temp folder. Following command will reset same as server.

    git reset --hard HEAD
    git clean -f
    git pull
    

    If you want to keep your changes , and remove recent commits

    git reset --soft HEAD^
    git pull
    
    0 讨论(0)
  • 2020-11-21 07:41

    Say we want to remove commits 2 & 4 from the repo.

    commit 0 : b3d92c5
    commit 1 : 2c6a45b
    commit 2 : <any_hash>
    commit 3 : 77b9b82
    commit 4 : <any_hash>
    

    Note: You need to have admin rights over the repo since you are using --hard and -f.

    • git checkout b3d92c5 Checkout the last usable commit.
    • git checkout -b repair Create a new branch to work on.
    • git cherry-pick 77b9b82 Run through commit 3.
    • git cherry-pick 2c6a45b Run through commit 1.
    • git checkout master Checkout master.
    • git reset --hard b3d92c5 Reset master to last usable commit.
    • git merge repair Merge our new branch onto master.
    • git push -f origin master Push master to the remote repo.
    0 讨论(0)
  • 2020-11-21 07:42

    Reset on local branch

    git reset --hard HEAD~<Number of commit> So git reset --hard HEAD~3
    

    Force push to origin

    git push -f origin
    
    0 讨论(0)
  • 2020-11-21 07:43

    What I do usually when I commit and push (if anyone pushed his commit this solve the problem):

    git reset --hard HEAD~1
    
    git push -f origin
    

    hope this help

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