如何合并Git分支中的特定文件

时间:2013-08-07 23:22:18

标签: git git-merge

我有2个git分支branch1和branch2,我想将branch2中的file.py合并到branch1中的file.py中,并且只将该文件合并。

本质上我只想处理branch1中的file.py但想要利用merge命令。这样做的最佳方式是什么?

11 个答案:

答案 0 :(得分:167)

已接受的答案已提到使用git checkout。现在,来自file.py的{​​{1}}中的内容可能不再适用于branch2。这种情况需要挑选一些变化并留下其他变化。因此,要完全控制使用branch1开关执行交互式合并:

--patch

$ git checkout --patch branch2 file.py 手册页中的交互模式部分说明了要使用的密钥:

git-add(1)

split命令特别有用。

答案 1 :(得分:96)

正如评论中所指出的,这在技术上并不是“合并”,这就是下面的链接使用引号的原因。但是,我很少需要像其他海报所描述的那样复杂的东西,所以我认为对于OP的问题,如果不是一个合适的解决方案是有用的。

查看此页面:Git Tip: How to "Merge" Specific Files from Another Branch它提供了最简单的方法,恕我直言。

基本上,将它应用于您的情况:

$ git checkout branch1 # i.e. make sure you're in branch1
$ git checkout branch2 file.py

简单易用,file.py现在在branch1。

答案 2 :(得分:14)

file.pybranch2的所有修改是否都在自己的提交中,与其他文件的修改分开?如果是这样,您只需cherry-pick进行更改:

git checkout branch1
git cherry-pick <commit-with-changes-to-file.py>

否则,merge不会对各个路径进行操作...您也可以从git diff和{{1}创建file.pybranch2次更改补丁他们到git apply

branch1

答案 3 :(得分:10)

其他目前的答案都不会真正&#34;合并&#34;这些文件,就好像您使用的是merge命令一样。 (最好它们会要求你手动选择差异。)如果你真的想利用来自共同祖先的信息进行合并,你可以遵循一个基于"Advanced Merging" sectiongit merge-file的程序。 git参考手册。

对于此协议,我假设您想要合并文件&#39; path / to / file.txt&#39;从origin / master到HEAD - 根据需要进行修改。 (您不必位于存储库的顶级目录中,但它会有所帮助。)

# Find the merge base SHA1 (the common ancestor) for the two commits:
git merge-base HEAD origin/master

# Get the contents of the files at each stage
git show <merge-base SHA1>:path/to/file.txt > ./file.common.txt
git show HEAD:path/to/file.txt > ./file.ours.txt
git show origin/master:path/to/file.txt > ./file.theirs.txt

# You can pre-edit any of the files (e.g. run a formatter on it), if you want.

# Merge the files
git merge-file -p ./file.ours.txt ./file.common.txt ./file.theirs.txt > ./file.merged.txt

# Resolve merge conflicts in ./file.merged.txt
# Copy the merged version to the destination
# Clean up the intermediate files

http://localhost/projectname/应该使用所有默认合并设置进行格式化等。

另请注意,如果您的&#34;我们的&#34;是工作副本版本,你不要过于谨慎,你可以直接在文件上操作:

git merge-base HEAD origin/master
git show <merge-base SHA1>:path/to/file.txt > ./file.common.txt
git show origin/master:path/to/file.txt > ./file.theirs.txt
git merge-file path/to/file.txt ./file.common.txt ./file.theirs.txt

答案 4 :(得分:5)

您可以class TestCase { transient private int x; private int y; public TestCase(int x, int y) { this.x = x; this.y = y; } public boolean equals(Object o) { if (o instanceof TestCase && (((TestCase) o).getXValue() == getYValue())) return true; else return false; } public int hashCode() { return x + y; } public int getXValue() { return x; } public int getYValue() { return y; } } class TestSet { public static void main(String[] args) { TestCase tc1 = new TestCase(4, 8); TestCase tc2 = new TestCase(4, 8); Set<TestCase> set = new HashSet<TestCase>(); set.add(tc1);//succeeds set.add(tc2);//succeeds System.out.println(set.add(tc2));//fails and gives us false. tc2 = new TestCase(4, 8); System.out.println(set.add(tc2));//succeeds and gives us true? System.out.println(set.size()); System.out.println(set.contains(tc1)); System.out.println(set.contains(tc2)); } } stash文件:

stash pop

答案 5 :(得分:2)

要仅合并来自branch2的file.py的更改,请使其他更改消失。

git checkout -B wip branch2
git read-tree branch1
git checkout branch2 file.py
git commit -m'merging only file.py history from branch2 into branch1'
git checkout branch1
git merge wip

合并永远不会查看任何其他文件。如果树木足够不同,您可能需要'-f'结帐。

请注意,这将使branch1看起来好像branch2的历史记录中的所有内容都已合并,这可能不是您想要的。上面第一个结账的更好版本可能是

git checkout -B wip `git merge-base branch1 branch2`

在这种情况下,提交消息可能也应该是

git commit -m"merging only $(git rev-parse branch2):file.py into branch1"

答案 6 :(得分:2)

我发现让我头疼最少的解决方案:

git checkout <b1>
git checkout -b dummy
git merge <b2>
git checkout <b1>
git checkout dummy <path to file>

完成此操作后,path to fileb2中的文件将与b1完全合并。

答案 7 :(得分:0)

我处于相同的情况,我想合并一个分支上的文件,该分支在2个分支上有很多提交。我尝试了上述多种方法,并且在互联网上发现了其他方法,但都失败了(因为提交历史很复杂),所以我决定按照自己的方式做(疯狂的方式)。

git merge <other-branch>
cp file-to-merge file-to-merge.example
git reset --hard HEAD (or HEAD^1 if no conflicts happen)
cp file-to-merge.example file-to-merge

答案 8 :(得分:0)

如果您只关心解决冲突而不是保留提交历史记录,则以下方法应该有效。假设您要将a.py b.py中的BRANCHA合并到BRANCHB中。首先,请确保BRANCHB中的所有更改都已提交或隐藏,并且没有未跟踪的文件。然后:

git checkout BRANCHB
git merge BRANCHA
# 'Accept' all changes
git add .
# Clear staging area
git reset HEAD -- .
# Stash only the files you want to keep
git stash push a.py b.py
# Remove all other changes
git add .
git reset --hard
# Now, pull the changes
git stash pop

git无法识别a.py b.py中存在冲突,但是如果实际上存在冲突,则存在合并冲突标记。使用第三方合并工具(例如VSCode),可以更加轻松地解决冲突。

答案 9 :(得分:0)

最简单的解决方案是:

git签出源分支的名称以及要添加到当前分支的特定文件的路径

git checkout sourceBranchName pathToFile

答案 10 :(得分:0)

Matthew Turner 的解决方案是最简单的,但如果 branch1 和 file 具有相同的名称,则会出现错误。在这种情况下,将第二行替换为

git checkout branch2 -- file.py

相关问题