恢复合并所做的更改

发布于 2024-12-18 12:17:32 字数 248 浏览 0 评论 0原文

开发人员对两个文件进行了一些小更改。但在这次提交期间,他遇到了合并冲突,删除了很多内容(可能没有最新版本)。然后它被推送到共享存储库,其他一些开发人员做了一些其他提交。

现在,我们注意到合并删除了重要文件,我们想将其恢复回来。
我怎样才能做到这一点而不丢失下一次提交的更改?

我试图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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静赏你的温柔 2024-12-25 12:17:32

git revert --mainline

通常:

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 revert --mainline

Usually:

git revert --mainline 1 dd8cbe3e4

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).
    • I can't find a good explanation of what the 1 means, but my guess is that 1,2,3... corresponds to a list of mappings to the commits immediately before dd8cbe3e4, 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/

满天都是小星星 2024-12-25 12:17:32

简而言之,警告:没有真正安全的方法来撤消合并,除非实际上重置分支到合并之前的提交。

现在让我解释一下并浏览现有的参考资料。

引用链接的答案来自 如何恢复错误的 git merge 提交

基本上,恢复合并将撤消数据更改,但不会撤消历史(图表)更改。因此,恢复错误的合并预计不会执行任何操作。

当然,重置分支将是最简单的方法,但如果合并的结果已经被推送到共享存储库,那么它就会有缺点(因为你'有效地重写已发表的历史)。

以下是要合并的细分

  • git merge(可以选择在解决冲突后提交)
  • 如果您立即发现想要将分支重置为合并前:

    git Reset HEAD@{1} 
         # 也可以选择传递 --hard 来重置工作树
    
  • 如果您稍后才发现,

    • 每次使用引用日志来查找合并之前的点。 (HEAD@{1} 是当前头引用的前一个值的缩写,但引用日志跟踪头引用值的有限历史记录)

      git reflog
      
    • 重置分支

      git reset HEAD@{n} # 替换引用日志条目索引
      
    • 可以选择重新调整/挑选合并后完成的提交

      gitcherry-pickHEAD@{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

Basically, reverting a merge will undo the data changes, but not the history (graph) changes. Therefore it is expected that reverting your faulty merge does nothing.

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:

    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
      
荒岛晴空 2024-12-25 12:17:32

另一种(更安全)的方法是在文件的最后一个版本和当前版本之间创建差异,然后通过复制和粘贴来恢复丢失的部分。

这总是有效的,不需要任何奇怪的命令行选项,并且不会篡改您应该保留的东西:-)

例如,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.

口干舌燥 2024-12-25 12:17:32

简而言之,您可以执行 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文