Delete commits from a branch in Git

后端 未结 30 2014
醉话见心
醉话见心 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:41

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

    commit 0 : b3d92c5
    commit 1 : 2c6a45b
    commit 2 : 
    commit 3 : 77b9b82
    commit 4 : 
    

    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.

提交回复
热议问题