我有2个git分支:
branch1 branch2
我想合并所有的历史(多次提交)的文件。py在branch2到文件。py在branch1,只有该文件。
实际上,我只是想在branch1中的file.py上工作,但想利用merge命令。
最好的方法是什么?
我有2个git分支:
branch1 branch2
我想合并所有的历史(多次提交)的文件。py在branch2到文件。py在branch1,只有该文件。
实际上,我只是想在branch1中的file.py上工作,但想利用merge命令。
最好的方法是什么?
当前回答
如果您只关心解决冲突,而不关心保存提交历史记录,那么下面的方法应该是有效的。假设你想把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,可以更轻松地解决冲突。
其他回答
我所做的有点手动,但我:
正常合并分支;使用revert恢复合并; 检出我所有的文件到HEAD~1,即它们的状态在 合并提交; 重设我的提交隐藏这个黑客从 提交历史。
丑吗?是的。容易记住吗?也没错。
我发现的最不让我头疼的方法是:
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 -
如果您只关心解决冲突,而不关心保存提交历史记录,那么下面的方法应该是有效的。假设你想把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,可以更轻松地解决冲突。
为了只合并来自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"