我有2个git分支branch1和branch2,我想将branch2中的file.py合并到branch1中的file.py中,并且只将该文件合并.
本质上我只想处理branch1中的file.py但想要利用merge命令.做这个的最好方式是什么?
pdp*_*pdp 187
已接受的答案已提到使用file.py
.现在,有可能是在内容上--patch
从git-add(1)
不再适用于file.py
.这种情况需要挑选一些变化并留下其他变化.因此,要完全控制使用--patch
开关执行交互式合并:
$ git checkout --patch branch2 file.py
Run Code Online (Sandbox Code Playgroud)
手册页中的交互模式部分用于git-add(1)
说明要使用的键:
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk nor any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
Run Code Online (Sandbox Code Playgroud)
split命令特别有用.
Мат*_*нер 101
正如评论中所指出的,这在技术上并不是"合并",这就是下面的链接使用引号的原因.但是,我很少需要像其他海报所描述的那样复杂的东西,所以我认为如果不是一个适当的OP解决方案,这对OP来说是有用的.
看看这个页面:Git提示:如何从另一个分支"合并"特定文件它提供了最简单的方法,恕我直言.
基本上,将它应用于您的情况:
$ git checkout branch1 # ensure in branch1 is checked out and active
$ git checkout branch2 file.py
Run Code Online (Sandbox Code Playgroud)
简单易用,file.py现在在branch1中.
小智 15
是否所有修改都file.py
在branch2
自己的提交中,与其他文件的修改分开?如果是这样,您可以简单地cherry-pick
进行更改:
git checkout branch1
git cherry-pick <commit-with-changes-to-file.py>
Run Code Online (Sandbox Code Playgroud)
否则,merge
不超过单个路径操作...你还不如干脆创建一个git diff
补丁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
Run Code Online (Sandbox Code Playgroud)
R.M*_*.M. 14
其他当前答案都不会实际"合并"文件,就像您使用merge命令一样.(最好它们会要求你手动选择差异.)如果你真的想利用来自共同祖先的信息进行合并,你可以按照git 的"高级合并"部分中的步骤进行操作.参考手册.
对于这个协议,我假设您想要将origin/master中的文件'path/to/file.txt'合并到HEAD中 - 根据需要进行修改.(您不必位于存储库的顶级目录中,但它会有所帮助.)
# 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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
小智 12
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 -
Run Code Online (Sandbox Code Playgroud)
Mak*_*gan 10
我发现最让我头疼的解决方案是:
git checkout <b1>
git checkout -b dummy
git merge <b2>
git checkout <b1>
git checkout dummy <path to file>
git branch -D dummy
Run Code Online (Sandbox Code Playgroud)
path to file
完成此操作后, in中的文件b2
就是与b1
.
你可以stash
和stash pop
文件:
git checkout branch1
git checkout branch2 file.py
git stash
git checkout branch1
git stash pop
Run Code Online (Sandbox Code Playgroud)
要仅合并来自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
Run Code Online (Sandbox Code Playgroud)
合并永远不会查看任何其他文件。如果树足够不同,您可能需要“-f”结帐。
请注意,这将使branch1看起来好像branch2历史记录中的所有内容都已合并,这可能不是您想要的。上面第一个结账的更好版本可能是
git checkout -B wip `git merge-base branch1 branch2`
Run Code Online (Sandbox Code Playgroud)
在这种情况下,提交消息可能也应该是
git commit -m"merging only $(git rev-parse branch2):file.py into branch1"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
103392 次 |
最近记录: |