499

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.

3

12 Answers 12

815

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 of the patch. 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.

20
  • 14
    This only works if you want to update a file. What if I want to add a new file from branch B to branch A?
    – Umair A.
    Feb 4, 2014 at 15:57
  • 37
    @UmairAshraf you should be able to add a new file from B to A by removing the --patch option.
    – bbak
    Feb 11, 2014 at 22:07
  • 10
    Hmmm... when I try this I get the message "no changes", but there clearly are changes. OK, I needed to be in the folder where the relevant file was. Edit: This could quite possibly be my favourite solution to a problem I've seen on stackoverflow :-D
    – mal
    Dec 11, 2014 at 8:59
  • 10
    I had to use git checkout --patch B -- f to get this to work.
    – user545424
    Jan 27, 2015 at 16:33
  • 9
    Just have to add that if you have multiple changes (hunks) in the file and you want to stage all of them, you can press a during interactive phase, instead of pressing y every time. Or use git checkout B -- f command instead. Nov 2, 2015 at 17:54
28

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.
3
  • 1
    To clarify the use of -- (empty argument label), git checkout docs: ARGUMENT DISAMBIGUATION say: "use git checkout -- <pathspec> if you want to checkout these paths out of the index." This is because you could have both a branch and a file/path by the same name. In such cases, rather than asking you to disambiguate whether the branch or the path should be checked out when both exist, git will opt to checkout the branch by default. However if -- preceeds git will checkout the file/path instead. Apr 25, 2018 at 21:21
  • git diff can be used as well to verify the differences.
    – user182917
    May 16, 2021 at 18:03
  • With git diff one could also use --cached while the file is staged. Btw, could you elaborate on the difference between the git checkout --patch method and this one? Is there any disadvantage of either method? I am more used to this one, but I'm wondering. Sep 27 at 11:56
20

Here's what I do in these situations. It's a kludge but it works just fine for me.

  1. Create another branch based off of your working branch.
  2. git pull/git merge the revision (SHA1) which contains the file you want to copy. So this will merge all of your changes, but we are only using this branch to grab the one file.
  3. Fix up any Conflicts etc. investigate your file.
  4. checkout your working branch
  5. Checkout the file commited from your merge.
  6. 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.)

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.

2
  • 6
    So we have to do all this just to merge one single file? Wouldn't it just be easier to copy and paste the file into the other branch
    – Robin
    Jun 19, 2018 at 18:46
  • 2
    @Robin probably not, because merging keeps the changes on the file, that differ between branch A and B. copying the file will overwrite any additional differences between your working branch A, and what you wanted to bring in from B, which may not contain those entrys/edits. e.g. suspect A has diverted from B to begin with, in other ways. Copying will overwite those differences.
    – blamb
    Nov 22, 2019 at 0:29
9

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

5
  • 22
    This overwrites the files, it doesn't merge them
    – Alex G
    Nov 12, 2019 at 8:24
  • For you it might, it depends what are you doing and what are you trying to achieve. The idea here is branch B is fork of A, you modify 4 files in B, but want to merge only 2 from B to A. Regular merge would merge all 4, here you can select. This may look as they were overridden because B contains essentially newer files. You need to support your experience with some evidence. Nov 12, 2019 at 19:27
  • 2
    I agree that it overwrites. i think you meant using the -p option in that command. Which then, overwrites any parts on your worktree file that have previously diverted from the branch your checking out from, prior to the patch changes, unfortunately.
    – blamb
    Nov 22, 2019 at 0:40
  • 1
    Well the idea was from 2009, chances are new version of git behaves differently and needs -p or anything else but back when I was posting it it was working for me, but again maybe I didn't care about the files being overridden, as latest version was what I needed Nov 22, 2019 at 17:57
  • 3
    Overwrites, and you don't.. care? Absolutely misleading, downvoted.
    – MrR
    Jun 22, 2021 at 16:28
9

You could use:

git merge-file

Tip: https://www.kernel.org/pub/software/scm/git/docs/git-merge-file.html

1
  • 5
    Im trying to merge one file only, but from another branch. I'm not seeing the option to merge from another branch git merge-file
    – blamb
    Nov 22, 2019 at 0:37
8

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>
1
  • 2
    I think you meant git checkout --patch master <filespec>.
    – chmaynard
    Oct 20, 2020 at 11:48
2

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.

3
  • Until around git 2.31 or 2.32, --cached and --3way were incompatible. I'm sort-of stuck at 2.30, but this appears to be exactly what I'm looking for. So I'm building git 2.41.0 from source. I'll confirm when I get it built. I really expected git read-tree --merge -u --prefix= to work, but it refuses.
    – Bob Kerns
    Jul 13 at 5:16
  • 1
    Can confirm—works great! Even better, you can do it on multiple paths, or entire directories, in a single command! I might suggest editing to add "..." after <file>. It was well worth building git from source to gain this capability. Thanks much!
    – Bob Kerns
    Jul 14 at 6:17
  • I'll also recommend this be the accepted answer, if the OP is still around to take note. The use case where interactive approval is desired, rather than a deal-breaker, can be met by combining 'git restore' and 'git add -p'
    – Bob Kerns
    Jul 14 at 6:36
1

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.

2
  • For me this generates a error: <file-path>: already exists in working directory
    – kontur
    Jan 6, 2017 at 9:48
  • You should specific file or file path at current directory. I'm use git diff Branch_A <file-path, filename> -- hash_commit > file_name.temp
    – R.Chatsiri
    Apr 4, 2019 at 9:40
1

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

0

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

2
  • Can you easily look into the patch for extra safety? May 25, 2020 at 12:56
  • not sure what I'm doing wrong but I cannot get git to produce the patch file. No error reported though
    – sergico
    Jan 26, 2021 at 14:40
0

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.
-2
git checkout <target_branch>
git checkout <source_branch> <file_path>
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.