git - Remove commit before HEAD

痞子三分冷 提交于 2019-12-20 03:27:12

问题


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 to remove)

How do I delete commit bar entirely from this branch but keep commit foo?


回答1:


git rebase -i HEAD~2

Will let you interactivly remove the commit

git rebase will remove all reference of that commit and change the id of the HEAD commit. Meaning people MAY have issues if they have branched from the old commit

git revert <commitID>

may be a better way to keep history




回答2:


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 <hash of the other branch's HEAD>

The graph would then look like this:

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


来源:https://stackoverflow.com/questions/32417174/git-remove-commit-before-head

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!