How to merge a specific directory or file in Git

How to merge a specific directory or file in Git

Think of the following scenarios:

  • There might be two branches with active development and one of the branch needs some specific (updated) file or directory.
  • You don't want to merge the complete branch but just need some specific file(s).

Similarly, there might be 'n' no. of scenarios where the common theme is that instead of merging the complete branch, all you need to do is merges specific file(s)/directory(s).

Using a smart trick which I like to call 'Selective checkout' can do the intended job

git checkout destination
git checkout source sub-directory/
git commit -am "Message."
git pull --rebase
git push

Using this git terminal trick now you can actually perform specific file/directory merge without merging the complete branch.