git merge specific files from another branch or revision

You have a perfect git branch model, neatly dividing code between the release and the development or trunk branch. Now suddenly you realise that you need to use the file from the other branch. But you can’t merge the branch for that sake !! WTF !

git checkout !! help !

  1. Make sure the file that you want to pull is clean (ie. that file shouldn’t be shown as modified in git status)
  2. $ git checkout <other-branch-name>/<commit-hash> -- path/to/the/file

You can now checkout the branch’s HEAD or that hash’s state of the file into your current working directory. You can now play with your file, add, commit. Done !

2 thoughts on “git merge specific files from another branch or revision”

  1. That does not merge changes, it overwrites the code in the current branch with changes from the selected branch.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.