Last Updated: October 04, 2020
· 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:

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

I read that here:

Related protips:

fatal: refusing to merge unrelated histories

1 Response
Add your response

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 ·