简体   繁体   English

在git合并冲突中,如何保留正在合并的版本?

[英]In git merge conflicts, how do I keep the version that is being merged in?

I have two local git branches on my machine - a branch called "v2" and a branch called "master". 我的机器上有两个本地git分支 - 一个名为“v2”的分支和一个名为“master”的分支。 I'm merging v2 into master while master is checked out and the head branch. 我正在将v2合并到master中,同时检查master和head branch。

I'd like to merge the "v2" branch into the "master" branch. 我想将“v2”分支合并到“master”分支中。 When I perform the merge, there are a number of conflicts that I must resolve one by one. 当我执行合并时,我必须逐个解决许多冲突。

For each conflict, how do I keep the "v2" branch file and not the "master" branch version of the file? 对于每个冲突,如何保留“v2”分支文件而不是文件的“主”分支版本?

The options presented to me by Git Tower for these types of conflicts are: Git Tower针对这些类型的冲突向我提出的选项是:

  • Mark FILENAME as Manually Resolved 将FILENAME标记为手动解析
  • Resolve by Keeping FILENAME 通过保持FILENAME解决
  • Resolve by Deleting FILENAME 通过删除FILENAME解决
  • Restore Their Version of FILENAME 恢复他们的FILENAME版本
  • Open in External App 在外部应用程序中打开

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). 根据我的理解,“保留”文件的选项意味着保留“v2”版本(正在合并的版本)和“删除”文件意味着不添加“v2”版本(而是保留现有的“主”版本)。 When I used the delete option, though, it actually deleted the file altogether from the repo. 但是,当我使用删除选项时,它实际上已从repo中删除了该文件。

How do I keep the "v2" branch file and not the "master" branch version of the file for these types of conflicts? 对于这些类型的冲突,如何保留“v2”分支文件而不是文件的“主”分支版本?

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. 如果您只想使用git tower,请按原样完成合并,然后签出该文件的其他分支版本。 Now stage and commit with amend - if possible. 现在进行修改 - 如果可能的话。

Git has been developed to be a command line tool. Git已经发展成为一个命令行工具。 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的设计作斗争。

Also, you could hook up something like Beyond Compare and choose "external tool" as is mentioned in your question. 此外,您可以连接Beyond Compare之类的内容,并选择问题中提到的“外部工具”。 There, you will have an option to choose the "theirs" side. 在那里,您可以选择“他们的”方面。

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手(我希望大师看起来与v2完全一样),我认为最简单的方法是:

  • Checkout the branch you want to merge 签出要合并的分支
  • Merge master into the branch with the ours strategy ours策略将master合并到分支中
  • Checkout master 结账大师
  • Merge the branch 合并分支

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会提供一种方法来实现这一目标。 :-( :-(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM