我有2个git分支:
branch1 branch2
我想合并所有的历史(多次提交)的文件。py在branch2到文件。py在branch1,只有该文件。
实际上,我只是想在branch1中的file.py上工作,但想利用merge命令。
最好的方法是什么?
我有2个git分支:
branch1 branch2
我想合并所有的历史(多次提交)的文件。py在branch2到文件。py在branch1,只有该文件。
实际上,我只是想在branch1中的file.py上工作,但想利用merge命令。
最好的方法是什么?
当前回答
我发现的最不让我头疼的方法是:
git checkout <b1>
git checkout -b dummy
git merge <b2>
git checkout <b1>
git checkout dummy <path to file>
git branch -D dummy
这样做之后,b2中文件路径下的文件就是与b1完全合并后的文件。
其他回答
虽然不是合并本身,但有时需要另一个分支上另一个文件的全部内容。Jason Rudolph的博客文章提供了一种将文件从一个分支复制到另一个分支的简单方法。应用如下技巧:
$ git checkout branch1 # ensure in branch1 is checked out and active
$ git checkout branch2 file.py
现在file.py在branch1中。
Matthew Turner的解决方案是最简单的,但是如果branch1和file有相同的名字就会给出一个错误。在这种情况下,将第二行替换为
Git checkout branch2——file.py
我发现的最不让我头疼的方法是:
git checkout <b1>
git checkout -b dummy
git merge <b2>
git checkout <b1>
git checkout dummy <path to file>
git branch -D dummy
这样做之后,b2中文件路径下的文件就是与b1完全合并后的文件。
Git checkout为此提供了一个——merge选项
Git checkout -merge branch2 file.py
使用此选项,将重新创建有冲突的合并。
否则,当一个新的合并发生时:
# Detach and overwrite file.py with content from branch2
git checkout --detach
git checkout branch2 file.py
# Amend changes and switch back
git commit --amend --no-edit
git checkout -
# Merge the detached branch back in
git merge --no-commit -
我在同样的情况下,我想合并一个文件从一个分支上有许多提交它在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