我有两个git分支branch1和branch2,我想将branch2中的file.py合并到branch1中的file.py中,并且只想合并该文件。
从本质上讲,我只想在branch1中使用file.py,但是想利用merge命令。做这件事最好的方法是什么?
发布于 2016-04-09 07:03:00
其他当前答案都不会真正“合并”这些文件,就像您正在使用merge命令一样。(充其量,它们将要求您手动选择差异。)如果你真的想利用来自共同祖先的信息进行合并,你可以按照git参考手册的"Advanced Merging" section中找到的步骤进行操作。
对于这个协议,我假设您想要将文件'path/ to /file.txt‘从modify /master合并到HEAD -modify中。(您不必在存储库的顶层目录中,但这会有所帮助。)
# 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
git merge-file应该使用所有默认的合并设置进行格式化等操作。
还请注意,如果您的“我们的”是工作副本版本,并且您不想过于谨慎,则可以直接对文件进行操作:
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
发布于 2013-08-08 07:39:01
是否所有在branch2
中对file.py
的修改都是在它们自己的提交中进行的,与对其他文件的修改是分开的?如果是这样的话,您可以简单地cherry-pick
这些更改:
git checkout branch1
git cherry-pick <commit-with-changes-to-file.py>
否则,merge
不能在单个paths...you上运行,还不如创建一个file.py
更改的branch2
补丁,并将其git apply
到branch1
git checkout branch2
git diff <base-commit-before-changes-to-file.py> -- file.py > my.patch
git checkout branch1
git apply my.patch
发布于 2016-05-06 03:07:15
您可以对文件执行stash
和stash pop
操作:
git checkout branch1
git checkout branch2 file.py
git stash
git checkout branch1
git stash pop
https://stackoverflow.com/questions/18115411
复制相似问题