我有2个git分支:
branch1 branch2
我想合并所有的历史(多次提交)的文件。py在branch2到文件。py在branch1,只有该文件。
实际上,我只是想在branch1中的file.py上工作,但想利用merge命令。
最好的方法是什么?
我有2个git分支:
branch1 branch2
我想合并所有的历史(多次提交)的文件。py在branch2到文件。py在branch1,只有该文件。
实际上,我只是想在branch1中的file.py上工作,但想利用merge命令。
最好的方法是什么?
当前回答
我所做的有点手动,但我:
正常合并分支;使用revert恢复合并; 检出我所有的文件到HEAD~1,即它们的状态在 合并提交; 重设我的提交隐藏这个黑客从 提交历史。
丑吗?是的。容易记住吗?也没错。
其他回答
最简单的解决方案是:
Git签出源分支的名称和我们想要添加到当前分支的特定文件的路径
git checkout sourceBranchName pathToFile
我在同样的情况下,我想合并一个文件从一个分支上有许多提交它在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
如果git checkout——patch branch2 file.py将被接受,那么我应该分享我们还可以使用:
git difftool <branch> [-- <file>]
([]表示可选。)
如果配置为diff.tool,像meld这样的合并工具将允许您使用图形界面手动合并两个文件。
它的一个缺点是,如果某个文件不存在于某个分支中,则无法复制或删除该文件。在这种情况下,我们需要git checkout branch2——file.py。
Git difftool也不保存历史。
为了只合并来自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
Merge甚至不会查看任何其他文件。如果树的差异足够大,您可能需要'-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"
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 -