我有2个git分支:
branch1 branch2
我想合并所有的历史(多次提交)的文件。py在branch2到文件。py在branch1,只有该文件。
实际上,我只是想在branch1中的file.py上工作,但想利用merge命令。
最好的方法是什么?
我有2个git分支:
branch1 branch2
我想合并所有的历史(多次提交)的文件。py在branch2到文件。py在branch1,只有该文件。
实际上,我只是想在branch1中的file.py上工作,但想利用merge命令。
最好的方法是什么?
当前回答
对branch2中的file.py的所有修改是否都在各自的提交中,与对其他文件的修改分开?如果是这样,你可以简单地选择更改:
git checkout branch1
git cherry-pick <commit-with-changes-to-file.py>
否则,merge不会在单独的路径上操作…你也可以从branch2中创建一个file.py更改的git diff补丁,然后git将它们应用到branch1:
git checkout branch2
git diff <base-commit-before-changes-to-file.py> -- file.py > my.patch
git checkout branch1
git apply my.patch
其他回答
我所做的有点手动,但我:
正常合并分支;使用revert恢复合并; 检出我所有的文件到HEAD~1,即它们的状态在 合并提交; 重设我的提交隐藏这个黑客从 提交历史。
丑吗?是的。容易记住吗?也没错。
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 -
虽然不是合并本身,但有时需要另一个分支上另一个文件的全部内容。Jason Rudolph的博客文章提供了一种将文件从一个分支复制到另一个分支的简单方法。应用如下技巧:
$ git checkout branch1 # ensure in branch1 is checked out and active
$ git checkout branch2 file.py
现在file.py在branch1中。
最简单的解决方案是:
Git签出源分支的名称和我们想要添加到当前分支的特定文件的路径
git checkout sourceBranchName pathToFile
我发现一些答案是有用的,但令人困惑,所以避免任何困惑的未来。我只是想帮助那些有同样困惑的人。
我不会使用branch1和branch2的名称,而是master(活动代码)和hotfix/abc(从master提取)和一个测试分支。
现在,我想将一些特定的文件从测试合并到hotfix/abc,因为不建议从测试或登台分支直接在master上合并。要做到这一点,我将做以下工作:
git checkout hotfix/abc git checkout --merge testing path/to/file1.php path/to/file2.js git add . git commit -m "Fixed specific issue" git push Now go to repo and make a pull request for hotfix/abc to master branch. If you don't know how to do that here I've a small tutorial on that. And if you generally want to learn how these branches and git work, I'd recommend you to watch this <= twenty minutes playlist. Now review your pull request with master and merge it. In case you see any conflicts, it's time to merge master into hotfix/abc. And resolve conflicts over there. And then again repeat step 3-5 followed by 7.
我还从参考教程中得到了帮助。
如果有用的话,竖起大拇指。快乐编码:)