我有两个分支(A和B),我想合并来自分支A的单个文件与来自分支B的相应单个文件。
当前回答
假设B是当前分支:
$ git diff A <file-path> > patch.tmp
$ git apply patch.tmp -R
注意,这只对本地文件应用更改。之后你需要做出承诺。
其他回答
我的编辑被拒绝了,所以我附上了如何从远程分支处理合并更改在这里。
如果你必须在错误的合并后这样做,你可以这样做:
# If you did a git pull and it broke something, do this first
# Find the one before the merge, copy the SHA1
git reflog
git reset --hard <sha1>
# Get remote updates but DONT auto merge it
git fetch github
# Checkout to your mainline so your branch is correct.
git checkout develop
# Make a new branch where you'll be applying matches
git checkout -b manual-merge-github-develop
# Apply your patches
git checkout --patch github/develop path/to/file
...
# Merge changes back in
git checkout develop
git merge manual-merge-github-develop # optionally add --no-ff
# You'll probably have to
git push -f # make sure you know what you're doing.
下面是我在这种情况下的做法。这是一个拼凑,但它对我来说很好。
在工作分支的基础上创建另一个分支。 git pull/git合并修订版(SHA1),其中包含您想要复制的文件。这将合并所有的更改,但我们只使用这个分支来获取一个文件。 解决任何冲突等调查你的文件。 检出您的工作分支 签出合并提交的文件。 提交它。
我试过修补,但我的情况太糟糕了。简而言之就是这样的
工作部门:A 实验分支:B(包含文件.txt,其中有我想折叠的变化)
git checkout A
根据A创建新分支:
git checkout -b tempAB
将B合并为tempAB
git merge B
复制合并的sha1哈希值:
git log
commit 8dad944210dfb901695975886737dc35614fa94e
Merge: ea3aec1 0f76e61
Author: matthewe <matthewe@matthewe.com>
Date: Wed Oct 3 15:13:24 2012 -0700
Merge branch 'B' into tempAB
签出你的工作分支:
git checkout A
签出您修复的文件:
git checkout 7e65b5a52e5f8b1979d75dffbbe4f7ee7dad5017 file.txt
你应该知道了。提交你的结果。
下面的命令将 (1)比较文件的正确分支,以掌握 (2)交互式地询问您应用哪些修改。
git checkout --patch master <fn>
这使用git的内部扩散工具。 也许有一点工作要做,但是直截了当。
#First checkout the branch you want to merge into
git checkout <branch_to_merge_into>
#Then checkout the file from the branch you want to merge from
git checkout <branch_to_merge_from> -- <file>
#Then you have to unstage that file to be able to use difftool
git reset HEAD <file>
#Now use difftool to chose which lines to keep. Click on the mergebutton in difftool
git difftool
#Save the file in difftool and you should be done.
你可以用:
git merge-file
提示:https://www.kernel.org/pub/software/scm/git/docs/git-merge-file.html