How to merge only specific files from another commit or branch

When you are working on some feature, it is not unlikely for you realize that not all the work done in another branch is now relevant except for the changes done to a few files. In such a situation, you probably want to retain only the files you need from the other branch without having to mess around with resolving merge conflicts with the other branch.

Fortunately 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 checkout the files you need from the branch, make any changes if you wish to and commit them. Since the checkout command is not restricted only to branchnames, you can do the same by checking out files from a particular commit as well

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

Ryan Sukale

Ryan is just a regular guy next door trying to manage his life and finances.

You may also like...

Leave a Reply

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