本文翻译自:How do I merge changes to a single file, rather than merging commits?
我有两个分支(A和B),我想将分支A中的单个文件与分支B中的相应单个文件合并。
#1楼
参考:https://stackoom.com/question/jFXv/如何将更改合并到单个文件-而不是合并提交
#2楼
I came across the same problem. 我遇到了同样的问题。 To be precise, I have two branches A
and B
with the same files but a different programming interface in some files. 确切地说,我有两个分支A
和B
具有相同的文件,但在某些文件中有不同的编程接口。 Now the methods of file f
, which is independent of the interface differences in the two branches, were changed in branch B
, but the change is important for both branches. 现在,文件f
的方法(与两个分支中的接口差异无关)在分支B
被改变,但是这两个分支的变化很重要。 Thus, I need to merge just file f
of branch B
into file f
of branch A
. 因此,我需要合并只是文件f
分支B
到文件f
分支的A
。
A simple command already solved the problem for me if I assume that all changes are committed in both branches A
and B
: 如果我假设所有更改都在A
和B
分支中提交,那么一个简单的命令就已经解决了我的问题:
git checkout A
git checkout --patch B f
The first command switches into branch A
, into where I want to merge B
's version of the file f
. 第一个命令切换到分支A
,进入我要合并文件f
B
版本的位置。 The second command patches the file f
with f
of HEAD
of B
. 第二个命令使用B
的HEAD
的f
修补文件f
。 You may even accept/discard single parts of the patch. 您甚至可以接受/丢弃补丁的单个部分。 Instead of B
you can specify any commit here, it does not have to be HEAD
. 您可以在此处指定任何提交,而不是B
,它不必是HEAD
。
Community edit : If the file f
on B
does not exist on A
yet, then omit the --patch
option. 社区编辑 :如果文件f
对B
不存在上A
还没有,那么忽略--patch
选项。 Otherwise, you'll get a "No Change." 否则,你将获得“无变化”。 message. 信息。
#3楼
Here's what I do in these situations. 这是我在这些情况下所做的。 It's a kludge but it works just fine for me. 这是一个kludge但它对我来说很好。
- Create another branch based off of your working branch. 根据您的工作分支创建另一个分支。
- git pull/git merge the revision (SHA1) which contains the file you want to copy. git pull / git合并包含要复制的文件的修订版(SHA1)。 So this will merge all of your changes, but we are only using this branch to grab the one file. 因此,这将合并您的所有更改,但我们只使用此分支来获取一个文件。
- Fix up any Conflicts etc. investigate your file. 修复任何冲突等调查您的文件。
- checkout your working branch 结账你的工作分支
- Checkout the file commited from your merge. 签出从合并中提交的文件。
- Commit it. 承诺吧。
I tried patching and my situation was too ugly for it. 我尝试修补,我的情况太难看了。 So in short it would look like this: 简而言之,它看起来像这样:
Working Branch: A Experimental Branch: B (contains file.txt which has changes I want to fold in.) 工作分支:实验分支:B(包含file.txt,其中包含我想要折叠的更改。)
git checkout A
Create new branch based on A: 基于A创建新分支:
git checkout -b tempAB
Merge B into tempAB 将B合并到tempAB中
git merge B
Copy the sha1 hash of the merge: 复制合并的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
Checkout your working branch: 结账你的工作分支:
git checkout A
Checkout your fixed-up file: 检查你的固定文件:
git checkout 7e65b5a52e5f8b1979d75dffbbe4f7ee7dad5017 file.txt
And there you should have it. 你应该拥有它。 Commit your result. 提交你的结果。
#4楼
My edit got rejected, so I'm attaching how to handle merging changes from a remote branch here. 我的编辑被拒绝,所以我在这里附上如何处理来自远程分支的合并更改。
If you have to do this after an incorrect merge, you can do something like this: 如果在不正确的合并后必须执行此操作,则可以执行以下操作:
# 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.
#5楼
You could use: 你可以使用:
git merge-file
Tip: https://www.kernel.org/pub/software/scm/git/docs/git-merge-file.html 提示: https : //www.kernel.org/pub/software/scm/git/docs/git-merge-file.html
#6楼
This uses git's internal difftool. 这使用git的内部difftool。 Maybe a little work to do but straight forward. 也许还有一点工作要做,但要直截了当。
#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.