ILoggable

A place to keep my thoughts on programming

January 31, 2011 geek

git: pulling individual files from another branch

Here's a not too uncommon task in git that I just can't seem to remember:

Merge specific files from another branch or revision

It's simply checkout out that file on top of the current branch, but I always forget the syntax and try something like git checkout <branch>:<file> which doesn't work. Then i think, oh, it's a copy, so i try git checkout <branch>:<file> . which checks the file out but in the current directory. So I think i'm on the right path and try <localpath> instead of the dot, but that just complains about error: pathspec '<localpath>' did not match any file(s) known to git. And then I finally wise up and google around and re-discover it's just:

git checkout <branch> <file1> <file2> ...

Yeah, that easy. Thanks to Jason Rudolph whose very familiar struggle google pointed me to this time around.

Oh and while I'm at it. <branch> in the above could be a commit (that nasty hex sig), so you can pick the file state from anywhere in time.

And what if that commit was on another branch how to you look at that log? git log <branch> of course. And you can even look at the log of branches not currently tracking locally with git log <remote>/<branch>, but be aware that while <remote>/<branch> does as the source for the above checkout, using the commit signature does not work until you are tracking that branch locally. If you try the error won't be very informative either:

$ git checkout b35e968bc9105041fd93d901bf8febe858d9847a src/mindtouch.core/service/S3StorageService.cs
error: pathspec 'b35e968bc9105041fd93d901bf8febe858d9847ar' did not match any file(s) known to git.
error: pathspec 'src/mindtouch.core/service/S3StorageService.cs' did not match any
file(s) known to git.

Well, hopefully I'll remember to look in my past posts next time I attempt this, because I'm sure i'll have forgotten again.

 

1 to “git: pulling individual files from another branch”

Trackbacks/Pingbacks

  1. […] Proceed with the commit, then simply revert to an earlier branch. Then (optionally) delete the offending branch (noting it will need to be a force delete given the branch isn’t merged). Simply use git checkout, to go back to a different branch, or even back to an earlier commit on this branch, as explained by iLoggable. […]

  1. Stop being a git – Start using Git! | Learning New Tricks says...

    […] Proceed with the commit, then simply revert to an earlier branch. Then (optionally) delete the offending branch (noting it will need to be a force delete given the branch isn’t merged). Simply use git checkout, to go back to a different branch, or even back to an earlier commit on this branch, as explained by iLoggable. […]

Leave a comment