git - Remove commit before HEAD

后端 未结 2 1855
执念已碎
执念已碎 2021-01-22 05:47

Okay I am new to Git and would like to know how to remove a commit before the HEAD. For instance-:

commit foo (This is the HEAD)

commit bar (This is what I want         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-22 06:25

    Another option is to checkout a new branch at the commit previous to the commit that you want to delete:

    git checkout -b new_branch HEAD~2
    

    And then to cherry pick the commits from the other branch to the new branch:

    git cherry-pick 
    

    The graph would then look like this:

    * 6a59727 (HEAD, new_branch) foo
    | * 15f07fd (master) foo
    | * 6bba064 bar
    |/  
    * dec804e baz
    

提交回复
热议问题