恢复合并所做的更改
开发人员对两个文件进行了一些小更改。但在这次提交期间,他遇到了合并冲突,删除了很多内容(可能没有最新版本)。然后它被推送到共享存储库,其他一些开发人员做了一些其他提交。
现在,我们注意到合并删除了重要文件,我们想将其恢复回来。
我怎样才能做到这一点而不丢失下一次提交的更改?
我试图git revert commitsha
,但它没有恢复更改。我需要恢复mergesha
吗?我怎样才能确定它?
The developer was commiting small changes to two files. But during this commit, he had a merge conflict which deleted a lot of stuff (probably didn't have the last up to date version). Then it was pushed to the shared repo and some other developers did some other commits.
Now, we noticed, that the merge deleted the important files, and we want to revert it back.
How can I do this without losing the changes from the next commits?
I was trying to git revert commitsha
, but it didn't bring the changes back. Do I need to revert back the mergesha
? How can I determine it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
git revert --mainline
通常:
其中:
dd8cbe3e4
是您想要撤消的错误合并提交,--mainline
告诉您之前的多个提交中的哪一个是要撤消的恢复(请记住,合并提交有多个父提交,您只能保留其中之一)。1
含义的良好解释,但我猜测1,2,3...
对应于映射列表到dd8cbe3e4
之前的提交,按升序时间顺序排序(最旧的在前 - 这通常是您想要恢复的内容)。来源:
http:// thezencoder.com/2013/09/05/how-to- Correctly-revert-a-bad-merge-in-git/
git revert --mainline
Usually:
Where:
dd8cbe3e4
is the bad merge commit you want to undo, and--mainline
tells you which of the multiple previous commits is the one to restore (remember, a merge commit has multiple parent commits and you can only keep one of them).1
means, but my guess is that1,2,3...
corresponds to a list of mappings to the commits immediately beforedd8cbe3e4
, sorted by ascending chronological order (oldest first - which is usually what you want to revert to).Source:
http://thezencoder.com/2013/09/05/how-to-correctly-revert-a-bad-merge-in-git/
简而言之,警告:没有真正安全的方法来撤消合并,除非实际上重置分支到合并之前的提交。
现在让我解释一下并浏览现有的参考资料。
引用链接的答案来自 如何恢复错误的 git merge 提交
当然,重置分支将是最简单的方法,但如果合并的结果已经被推送到共享存储库,那么它就会有缺点(因为你'有效地重写已发表的历史)。
以下是要合并的细分
git merge
(可以选择在解决冲突后提交)如果您立即发现想要将分支重置为合并前:
如果您稍后才发现,
每次使用引用日志来查找合并之前的点。 (
HEAD@{1}
是当前头引用的前一个值的缩写,但引用日志跟踪头引用值的有限历史记录)重置分支
可以选择重新调整/挑选合并后完成的提交
In short, WARNING: there is no real safe way to undo a merge except to actually reset the branch to the commit before the merge.
Let me explain and browse for an existing reference for now.
Quoting the linked answer from How do you revert a faulty git merge commit
Certainly, resetting the branch would be the most simple approach, but it has drawbacks if the result of the merge has already been pushed to a shared repo (because you're effectively rewriting published history).
Here is the breakdown
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:
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)reset the branch
optionally rebase/cherry-pick the commits done after the merge
另一种(更安全)的方法是在文件的最后一个版本和当前版本之间创建差异,然后通过复制和粘贴来恢复丢失的部分。
这总是有效的,不需要任何奇怪的命令行选项,并且不会篡改您应该保留的东西:-)
例如,Eclipse 有很好的工具来挑选每个单独的差异并将其复制到任一版本。只需使用“比较”菜单即可并排打开两个版本。
Another (more safe) approach is to create a diff between the last good and the current version of the file and then restore the lost parts by copy&paste.
This always works, doesn't need any odd command line options, and it doesn't tamper with things that you should leave alone :-)
Eclipse, for example, has good tools to cherry pick each individual difference and copy it to either version. Just use the "Compare" menu to open both versions side by side.
简而言之,您可以执行 git reset --soft
其中提交可以是
HEAD^
(前一个)、HEAD~2
(当前-2)、SHA 等。使用 --soft 所有更改都可以提交,所以你实际上可以更改提交。使用 --hard 时,所做的更改将全部丢失。
更改提交后,您必须使用 git push --force 强制将更改推送到共享存储库。
请注意,您需要告诉其他开发人员,他们应该将其存储库重新建立到共享存储库上。 (使用 git pull --rebase )。不过,他们可能会遇到一些合并冲突......请记住这一点。
In short, you can do a
git reset --soft <commit>
where commit can be
HEAD^
(previous),HEAD~2
(current-2), a SHA, etc.With --soft all the changes will be ready to commit, so you can actually change the commit. With --hard the changes will all be lost.
After you altered the commit you have to force push the changes to the shared repo with
git push --force
.Note that you will need to tell the other developers that they should rebase their repos onto the shared repo. (use
git pull --rebase
). They could get some merge conflicts though... Please keep that in mind.