【发布时间】:2015-12-01 17:46:35
【问题描述】:
好的,我是 Git 新手,想知道如何在 HEAD 之前删除提交。比如——:
commit foo (This is the HEAD)
commit bar (This is what I want to remove)
如何从这个分支中完全删除commit bar,但保留commit foo?
【问题讨论】:
标签: git git-commit
好的,我是 Git 新手,想知道如何在 HEAD 之前删除提交。比如——:
commit foo (This is the HEAD)
commit bar (This is what I want to remove)
如何从这个分支中完全删除commit bar,但保留commit foo?
【问题讨论】:
标签: git git-commit
git rebase -i HEAD~2
将让您以交互方式删除提交
git rebase 将删除该提交的所有引用并更改 HEAD 提交的 id。这意味着如果人们从旧提交分支出来,他们可能会遇到问题
git revert <commitID>
可能是保存历史的更好方法
【讨论】:
git log),您还可以使用 HEAD^ 或 HEAD~1 来表示“HEAD 之前的提交”。
git rebase 给出invalid upstream HEAD~2 的错误。 git revert 创建一个额外的提交,但不会从日志中删除我想要的提交。我想要做的是只删除第一次提交,而不是最近的一次。
另一种选择是在要删除的提交之前的提交处签出一个新分支:
git checkout -b new_branch HEAD~2
然后从另一个分支挑选提交到新分支:
git cherry-pick <hash of the other branch's HEAD>
图表将如下所示:
* 6a59727 (HEAD, new_branch) foo
| * 15f07fd (master) foo
| * 6bba064 bar
|/
* dec804e baz
【讨论】: