这位开发者在修改两个文件时,遇到了合并冲突,导致很多东西被删除(可能是因为没有最新的版本)。然后该提交被推送到共享仓库,并且其他开发者进行了其他的提交。
现在我们注意到这次合并删除了重要的文件,并希望将其还原。
如何在不丢失下一个提交更改的情况下进行还原?
我尝试使用 git revert commitsha
,但它没有恢复更改。我需要还原 mergesha
吗?如何确定这个值?
通常情况下:
git revert --mainline 1 dd8cbe3e4
位置:
dd8cbe3e4
是你想要撤消的错误合并提交,--mainline
告诉你要恢复的多个先前提交中的哪一个(记住,合并提交有多个父提交,你只能保留其中之一)。
1
意味着什么的好解释,但我猜1,2,3...
对应于在dd8cbe3e4
之前立即提交的提交的映射列表,按升序时间顺序排序(最旧的优先 - 这通常是您想要还原的内容)。来源:
http://thezencoder.com/2013/09/05/how-to-correctly-revert-a-bad-merge-in-git/
当然,将分支重置为最简单的方法,但如果合并的结果已经被推送到共享存储库中,则会有缺点(因为您实际上正在重写已发布的历史记录)。基本上,撤销合并将撤消数据更改,但不会撤消历史记录(图形)更改。因此,预计撤销错误的合并不会产生任何影响。
git merge <someref>
to merge (optionally commit after resolving conflicts)If you find out right away that you want to reset the branch to before the merge:
git reset HEAD@{1}
# optionally pass --hard to reset the working tree too
if you found out only later,
per-use the reflog to find the point before the merge. (HEAD@{1}
is short for the previous value of the current head reference, but the reflog tracks a limited history of values for the head reference)
git reflog
reset the branch
git reset HEAD@{n} # substitute reflog entry index
optionally rebase/cherry-pick the commits done after the merge
git cherry-pick HEAD@{1} # just an example. interactive tools will make this easier
pull --rebase
;(b)永远不要在合并提交中编辑合并冲突;(c)不要破坏合并;(d)启用 git-rerere
。 - sehegit reset --soft <commit>
,其中提交可以是HEAD^
(上一个)、HEAD~2
(当前-2)、SHA等。git push --force
强制推送更改到共享存储库。git pull --rebase
)。但是,他们可能会遇到一些合并冲突... 请记住这一点。