GIT - 如何合并分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5326894/
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
GIT - how to merge branches?
提问by Chvanikoff
We decided to use GIT in our company but now got a problem.. We have several branches with different features. Now what we need is to merge that branches and push it to Master. How shall we do that with autoreplace - we have branch-a, branch-b, branch-c - we need to get them all in Master but in case of repeated files the branch-b should be Major and branch-c - minor.
我们决定在我们公司使用 GIT,但现在遇到了一个问题。我们有几个具有不同功能的分支。现在我们需要的是合并这些分支并将其推送到 Master。我们应该如何使用自动替换来做到这一点 - 我们有分支 a、分支 b、分支 c - 我们需要将它们全部放在 Master 中,但在重复文件的情况下,分支 b 应该是主要的和分支 c - 次要的。
Upd:
更新:
branch-a: -file1 -file2 -file3 -file4
branch-b: -file1 -file5 -file6
branch-c: -file1 -file2 -file7 -file8
we need in result:
我们需要结果:
Master: -file1 (from b) -file2 (from a) -file3 (from a) -file4 (from a) -file5 (from b) -file6 (from b) -file7 (from c) -file8 (from c)
回答by Adam Dymitruk
Perform an octopus merge and don't commit '--no-commit'. From master
执行章鱼合并,不要提交“--no-commit”。来自主人
git merge --no-commit branch-a branch-b branch-c
resolve any conflicts, then if as you want, take a specific version of a file and commit:
解决任何冲突,然后根据需要,获取文件的特定版本并提交:
git checkout branch-b -- file3
repeat for other specific files.
对其他特定文件重复。
git add . -A
git commit
This is the way your custom work-flow would work.
这是您的自定义工作流程的工作方式。
Hope this helps.
希望这可以帮助。
回答by Mark Longair
I haven't tested this, but this might do what you want:
我还没有测试过这个,但这可能会做你想要的:
git checkout master
git merge -s recursive -X theirs branch-c
git merge -s recursive -X theirs branch-a
git merge -s recursive -X theirs branch-b
This is using the recursive
merge strategy, but saying that if there are any conflicts when merging, to always resolve them in favour of the branch you're merging into your current branch. To find the documentation for this, look at the section on the recursive merge strategy in the git merge man page, and the ours
and theirs
options to that strategy.
这是使用recursive
合并策略,但说如果合并时有任何冲突,请始终解决它们以支持您合并到当前分支的分支。要找到这个文件,看看上的递归合并策略部分混帐合并人页,并ours
和theirs
选项这一战略。
The problem with this is that if the changes you've made to the files in each branch don't conflict, you'll have some of the changes from one branch and one in the other. If that's not what you want (and I concede that I don't quite understand your workflow in the first place) you might have to do, for example:
这样做的问题是,如果您对每个分支中的文件所做的更改不冲突,那么您将在一个分支和另一个分支中进行一些更改。如果这不是您想要的(我承认我一开始不太了解您的工作流程),您可能必须这样做,例如:
git merge --no-commit branch-c
... and then update the index with each file version that you want manually before committing that merge commit. After running that command, you could find all the files which are present on both branches with:
...然后在提交合并提交之前手动更新每个文件版本的索引。运行该命令后,您可以使用以下命令找到两个分支上存在的所有文件:
comm -1 -2 <(git ls-tree -r --name-only master|sort) <(git ls-tree -r --name-only branch-c|sort) > common.txt
... and then pick the branch-c
version for each one with:
...然后branch-c
为每个版本选择:
xargs -n 1 git checkout branch-c -- < common.txt
... and then committing with git commit
as usual.
...然后git commit
像往常一样提交。
Just to reiterate, I'm pretty sure I'd neverwant to do this - git's default merge behaviour almost always does what I want, only leaving genuine conflicts to resolve.
重申一下,我很确定我永远不想这样做——git 的默认合并行为几乎总是按照我的意愿行事,只留下真正的冲突来解决。
回答by Ivan
file copy - a simple alternative
文件复制 - 一个简单的替代方案
One way to do this would be to checkout the master branch. In separate repositories, checkout the minor and major branch files. Copy in the minor files and then the major files, so that the major files overwrite the minor files. Then commit the result.
一种方法是检查主分支。在单独的存储库中,签出次要和主要分支文件。先复制小文件,再复制大文件,使大文件覆盖小文件。然后提交结果。
The file copy will even simply allow you to decide on a case by case basis (if necessary) whether you really want the major file to overwrite the minor file or not, and a good file copy program will give you dates and filesizes of the files and possibly previews too, to help you make an informed decision.
文件复制甚至可以让您根据具体情况(如有必要)决定是否真的希望主文件覆盖次文件,一个好的文件复制程序将为您提供文件的日期和文件大小可能还有预览,以帮助您做出明智的决定。
This might not be the best way of doing things for your particular case. Just treat it as an extra tool in your toolbox. Of course, if merging is what you want to do, rather than just overwriting, then gitis still the tool to use, helped along with a mergetool such as kdiff3.
对于您的特定情况,这可能不是最好的处理方式。只需将其视为工具箱中的额外工具即可。当然,如果您想做的是合并,而不仅仅是覆盖,那么git仍然是可以使用的工具,并与诸如 kdiff3 之类的合并工具一起提供帮助。