How to merge only specific files from another commit or branch

When you are work­ing on some fea­ture, it is not unlikely for you real­ize that not all the work done in another branch is now rel­e­vant except for the changes done to a few files. In such a sit­u­a­tion, you prob­a­bly want to retain only the files you need from the other branch with­out hav­ing to mess around with resolv­ing merge con­flicts with the other branch.

For­tu­nately doing so is pretty easy

git checkout branchName path/to/file1 path/to/file2
# Now modify the files locally if needed
# Add and commit

Since you dont need other files, you can just check­out the files you need from the branch, make any changes if you wish to and com­mit them. Since the check­out com­mand is not restricted only to branch­names, you can do the same by check­ing out files from a par­tic­u­lar com­mit as well

git checkout commitSHA path/to/file1 path/to/file2
# Now modify the files locally if needed
# Add and commit

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *