How to merge specific files from Git branches

I have 2 git branches branch1 and branch2, and I want to merge the .py file in branch2 to file.py in branch1 and only this file.

In essence, I just want to work with file.py in branch1, but I want to use the merge command. What is the best way to do this?

+159
git git-merge
Aug 07 '13 at 23:22
source share
6 answers

The accepted answer already mentions the use of git checkout . Now in file.py from branch2 may be content that is no longer applicable in branch1 . Such a situation will require the selection of some changes and the abandonment of others. Therefore, to have full control, do an interactive merge using the --patch switch:

 $ git checkout --patch branch2 file.py 

The interactive mode section of the man page for git-add(1) describes the keys that will be used:

 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 

The split command is especially useful.

+184
Oct. 16 '15 at 10:21
source share

As pointed out in the comments, this is technically not a β€œmerge”, so the link below uses quotation marks. However, I rarely need something complicated, as other posters describe, so I find this a useful, if not right, solution to the OP question.

Take a look at this page: Git Tip. How to "merge" certain files from another section. This is the easiest way to do this, IMHO.

Basically, to apply it to your situation:

 $ git checkout branch1 # ie make sure you're in branch1 $ git checkout branch2 file.py 

Easy peasy, file.py is now in branch1.

+100
Aug 07 '13 at
source share

Are all changes to file.py in branch2 in their own entries, separate from changes in other files? If so, you can just cherry-pick make the changes:

 git checkout branch1 git cherry-pick <commit-with-changes-to-file.py> 

Otherwise, merge does not work on separate paths ... you can simply create the git diff patch of file.py changes from branch2 and git apply to branch1 :

 git checkout branch2 git diff <base-commit-before-changes-to-file.py> -- file.py > my.patch git checkout branch1 git apply my.patch 
+14
Aug 07 '13 at 23:39
source share

None of the other current answers actually β€œflies” files, as if you were using the merge command. (At best, you will need to manually select diff.) If you really want to take advantage of merging using information from a common ancestor, you can follow the procedure based on the one found in the Advanced Merging section of the git reference manual.

In this protocol, I assume that you want to merge the file 'path / to / file.txt' from origin / master into HEAD - change if necessary. (You do not have to be in the top folder of your repository, but it helps.)

 # 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 (eg 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 

git merge-file should use all your default merge settings for formatting, etc.

Also note that if your β€œours” is a working version and you don’t want to be too careful, you can directly work with the 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 
+11
Apr 08 '16 at 23:02
source share

You can stash and stash pop create a file:

 git checkout branch1 git checkout branch2 file.py git stash git checkout branch1 git stash pop 
+6
May 05 '16 at 19:07
source share

To merge only the changes from branch2 file.py , change the rest of the changes.

 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 

A merge will never look at any other file. You may need β€œ-f” statements if the trees are different enough.

Note that this will leave branch1 looking as if everything in the history of branch2 were merged to this point, which might not be what you want. The best version of the first check above is probably

 git checkout -B wip `git merge-base branch1 branch2` 

in this case the commit message should also be

 git commit -m"merging only $(git rev-parse branch2):file.py into branch1" 
+2
Aug 08 '13 at 2:03
source share



All Articles