Pulling specific files from a commit using Git

I ran into a situation today where I needed to grab a specific version of two files from one branch in our repository and merge it into another branch. Normally I would use cherry-pick for this sort of thing but when you are working with developers who arent used to the way of Git, that can be hard. Especially in our situation where most of them are used to Subversion so they make changes to many different files over the period of hours or worse yet, days before they commit their changes.

So in this particular case, I found the commit where the files I needed lived and wouldnt you know it, there were about 12 files in this commit and I only needed 2 of them. So a regular cherry-pick wouldnt work for me in this situation. So how do we pull only the files we need out of that commit and into our current branch? We use our old friend, git checkout to specify the commit and the file path/names we want and check them out into our current branch like this:

git checkout 6dd17ab5c81b53caa803fb2fae245a89367a2b2e path/to/myfile.cfm

That grabs the file referenced from the commit specified by the SHA and brings them into our current branch.

Super easy!

2 thoughts on “Pulling specific files from a commit using Git”

Leave a Reply

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


*