Merge specific files from a branch in Git

Updated by Tom on

When working on a large codebase or a codebase with many developers working on new features, there may come a time when you want to merge specific files from a branch rather than a whole commit or all the changes on the branch. Thankfully, this is easy to do!

Merge specific files

To merge specific files from a branch, use git checkout branch-name file-name. For example:

git checkout develop src/somefile.js src/somefile2.js
Merge a specific file from a branch using git example

To break this down, we're merging src/somefile.js and somefile2.js from develop branch and merging it into our current active branch. So, as you can see, this is both simple and powerful!

How is this different from cherry-pick?

Cherry-picking from a branch is different in that this merges specific files only, whereas cherry-picking merges a specific commit and the changes associated with that commit.

To cherry-pick from a branch, use git cherry-pick e26ad04 where e26ad04 is your commit ID.

Wrapping up

In general, I would recommend cherry-picking your commits or attempt a standard merge, but if you require a bit more granular control, then it's possible, and simple, to merge specific files into a branch using git.

If you found this useful or if you have any feedback or questions, feel free to follow or comment on Twitter.

If you enjoyed the article, please consider sharing it on Twitter or Facebook