Reputation: 8193
I have two branches (A and B) and I want to merge a single file from branch A with a corresponding single file from Branch B.
Upvotes: 524
Views: 287208
Reputation: 8575
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. 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. Thus, I need to merge just file f
of branch B
into file f
of branch A
.
A simple command already solved the problem for me if I assume that all changes are committed in both branches A
and 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
. The second command patches the file f
with f
of HEAD
of B
. You may even accept/discard single parts (called "hunks") of the patch interactively. Instead of B
you can specify any commit here, it does not have to be HEAD
.
Community edit: If the file f
on B
does not exist on A
yet, then omit the --patch
option. Otherwise, you'll get a "No Change." message.
Upvotes: 838
Reputation: 3633
To merge
a single file, with merge conflict from another branch, you could do it like this:
Branch A:
file_01
file_02
file_03
Branch B:
file_01
file_02
file_03
file_04
file_05
Commands:
git checkout A
git merge B
You will get message about what files you have to manually resolve because they have overlapping code. Files in current branch(A) will be changed and it will contain both code, own code(A), code from B, and it will have <<<<<< HEAD
, ======
, >>>>>> B
; Git merge
notations.
git reset
git add file_01
git clean -f -d
You have unstaged all files that were affected when merging B onto A and added only file that you want to merge, file_01
file. After that you delete all unstaged files. Now you can edit file_01
file to get the right code that you need or you can leave it as is. For Git itself, it will not matter if you leave the file unresolved, it will contain overlapping code form both branches with Git merge notations. But you will have to commit
, because Git is still in a 'state' of merge and it will close when you commit the changes.
git commit -m "Merge file_01 only"
Note that you can add
multiple files before commit
, does not have to be only one.
Upvotes: 0
Reputation: 33
I've seen others suggesting git checkout -- <file>
, and while that overwrites the file instead of merging, I found that using a 'local changes' diff tool in my IDE is more than sufficient (IMHO) to compare the two versions.
Upvotes: 0
Reputation: 3202
I found this approach simple and useful: How to "merge" specific files from another branch
As it turns out, we’re trying too hard. Our good friend git checkout is the right tool for the job.
git checkout source_branch <paths>...
We can simply give git checkout the name of the feature branch A and the paths to the specific files that we want to add to our master branch.
Please read the whole article for more understanding
UPDATE 5 years after initially posting this: I've read the link attached again and now I think the author meant to just "bring-in" files to master from another branch whether it does overwrite or not.
I do remember doing some testing myself and it seemed to work. At the moment don't have time to re-test his idea or test my new idea, but it could be something like:
Perform regular merge without immediate commit, stage only files wanted to be merged, discard all modified files that are not staged, and finally commit only staged/wanted files.
Upvotes: 10
Reputation: 1608
The following will have conflicts recorded in the index just as they would be when merging branches.
export other=<sha1 or branch name of the other commit>
export base=$(git merge-base @ $other)
git diff $base $other <file> [<file> ...] | git apply --cached --3way
The magic here is the --cached --3way
mode of apply
. A successful 3-way application of a patch depends on having access to the blobs of the wanted file from both the base commit and the other commit, which is guaranteed if we generate the patch with a diff command run against the same local repository.
This approach is different from, and IMO superior to both checkout --patch
where conflicts need to be resolved interactively, and merge-file
where conflicts are written in diff3 mode into the working directory. Some IDEs don't support inline worktree conflict markers, but most can very well work with conflicts recorded in the index.
Upvotes: 4
Reputation: 1719
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.
Upvotes: 0
Reputation: 494
You could use:
git merge-file
Tip: https://www.kernel.org/pub/software/scm/git/docs/git-merge-file.html
Upvotes: 10
Reputation: 878
The following command will (1) compare the file of the correct branch, to master (2) interactively ask you which modifications to apply.
git checkout --patch master <fn>
Upvotes: 9
Reputation: 309
This uses git's internal 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.
Upvotes: 30
Reputation: 1971
git checkout <target_branch>
git checkout <source_branch> <file_path>
Upvotes: -2
Reputation: 1
I will do it as
git format-patch branch_old..branch_new file
this will produce a patch for the file.
Apply patch at target branch_old
git am blahblah.patch
Upvotes: 0
Reputation: 3117
You can checkout the old version of the file to merge, saving it under a different name, then run whatever your merge tool is on the two files.
eg.
git show B:src/common/store.ts > /tmp/store.ts
(where B is the branch name/commit/tag)
meld src/common/store.ts /tmp/store.ts
Upvotes: 1
Reputation: 37
Assuming B is the current branch:
$ git diff A <file-path> > patch.tmp
$ git apply patch.tmp -R
Note that this only applies changes to the local file. You'll need to commit afterwards.
Upvotes: 2
Reputation: 1140
Here's what I do in these situations. It's a kludge but it works just fine for me.
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.)
git checkout A
Create new branch based on A:
git checkout -b tempAB
Merge B into tempAB
git merge B
Copy the sha1 hash of the merge:
git log
commit 8dad944210dfb901695975886737dc35614fa94e
Merge: ea3aec1 0f76e61
Author: matthewe <[email protected]>
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.
Upvotes: 22