在 git merge 冲突中,如何保留正在合并的版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13885390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In git merge conflicts, how do I keep the version that is being merged in?
提问by Tim Jahn
I have two local git branches on my machine - a branch called "v2" and a branch called "master". I'm merging v2 into master while master is checked out and the head branch.
我的机器上有两个本地 git 分支 - 一个名为“v2”的分支和一个名为“master”的分支。我正在将 v2 合并到 master 中,同时检出 master 和 head 分支。
I'd like to merge the "v2" branch into the "master" branch. When I perform the merge, there are a number of conflicts that I must resolve one by one.
我想将“v2”分支合并到“master”分支中。当我执行合并时,有许多冲突我必须一一解决。
For each conflict, how do I keep the "v2" branch file and not the "master" branch version of the file?
对于每个冲突,我如何保留“v2”分支文件而不是文件的“master”分支版本?
The options presented to me by Git Tower for these types of conflicts are:
对于这些类型的冲突,Git Tower 提供给我的选项是:
- Mark FILENAME as Manually Resolved
- Resolve by Keeping FILENAME
- Resolve by Deleting FILENAME
- Restore Their Version of FILENAME
- Open in External App
- 将 FILENAME 标记为手动解析
- 通过保留 FILENAME 解决
- 通过删除 FILENAME 解决
- 恢复他们的 FILENAME 版本
- 在外部应用程序中打开
From my understanding, the option to "keep" the file meant keeping the "v2" version (the one being merged in) and "deleting" the file meant not adding the "v2" version (but instead keeping the existing "master" version). When I used the delete option, though, it actually deleted the file altogether from the repo.
根据我的理解,“保留”文件的选项意味着保留“v2”版本(被合并的版本)和“删除”文件意味着不添加“v2”版本(而是保留现有的“主”版本)。但是,当我使用删除选项时,它实际上从存储库中完全删除了该文件。
How do I keep the "v2" branch file and not the "master" branch version of the file for these types of conflicts?
对于这些类型的冲突,如何保留“v2”分支文件而不是文件的“master”分支版本?
回答by Adam Dymitruk
Even though you are using Git Tower, you can drop down to the command line and use
即使您使用的是 Git Tower,您也可以下拉到命令行并使用
git checkout --theirs file.txt
Here some docs about it:
这里有一些关于它的文档:
http://gitready.com/advanced/2009/02/25/keep-either-file-in-merge-conflicts.html
http://gitready.com/advanced/2009/02/25/keep-either-file-in-merge-conflicts.html
If you want to ONLY use git tower, complete the merge as is, then checkout the other branch's version of that file. Now stage and commit with amend- if possible.
如果您只想使用 git Tower,请按原样完成合并,然后检出该文件的另一个分支版本。现在登台并提交修改- 如果可能的话。
Git has been developed to be a command line tool. With every other tooling that I've ever used, I always had a gap in functionality. I chose to embrace instead of fight the design of Git.
Git 已被开发为一个命令行工具。对于我使用过的所有其他工具,我总是在功能上存在差距。我选择拥抱而不是对抗 Git 的设计。
Also, you could hook up something like Beyond Compare and choose "external tool" as is mentioned in your question. There, you will have an option to choose the "theirs" side.
此外,您可以连接诸如 Beyond Compare 之类的东西并选择您的问题中提到的“外部工具”。在那里,您可以选择“他们的”一方。
回答by John Szakmeister
If you're looking to keep v2 hands down (I want master to look exactly like v2), I think the easiest way is to:
如果您希望保持 v2 不受影响(我希望 master 看起来与 v2 完全一样),我认为最简单的方法是:
- Checkout the branch you want to merge
- Merge master into the branch with the
ours
strategy - Checkout master
- Merge the branch
- 签出要合并的分支
- 使用
ours
策略将master 合并到分支中 - 结账师傅
- 合并分支
It would look like this:
它看起来像这样:
git checkout v2
git merge -s ours master
git checkout master
git merge v2
If you just want this type of resolution to happen only on conflicts, then you can do:
如果您只希望这种类型的解决方案仅在冲突时发生,那么您可以执行以下操作:
git checkout master
git merge -s recursive -Xtheirs v2
You can read up on merge strategies in the git documentation here.
您可以在此处的 git 文档中阅读合并策略。
UPDATE: unfortunately, I don't think Git Tower exposes a way to do this yet. :-(
更新:不幸的是,我认为 Git Tower 还没有公开一种方法来做到这一点。:-(