Last Updated: October 04, 2020
·
78.36K
· shekhardesigner

Git - Merge a single file from one branch to another.

How do you merge just one or few selected files from one branch to another without going through the branch merge, manually brute force or ete etc hacks?

Well I had my head scratching over for few hours and found nice way to do that.

git checkout seems to be the best choice we have.

Assume you are in master branch and want to merge from dev_i3 branch, use this syntax:
git checkout <BRANCH_NAME> <PATH_OF_FILE>

Eg. git checkout dev_i3 views/shared/nav.cshtml

If you wish to merge multiple selective files at once:
git checkout <BRANCH_NAME> <PATH_OF_FILE_1> <PATH_OF_FILE_2> <PATH_OF_FILE_3>

Once you perform this, that particular file from dev_i3 branch would be added to master branch.

I read that here: http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/

Related protips:

fatal: refusing to merge unrelated histories

1 Response
Add your response

Hello
I have tried that and result is not expected.
What I try to do is:

I have a very old file1 in branchA that contains a couple of functions that I need
I have the latest version of file1 in branchB

I need to merge (i do not know if this is the correct concept, maybe rebase?) some differences (the functions that i need) that are in the version of file in branchA to the branchB.

With this solution you propoused, there is a substitution but not a merge.

over 1 year ago ·