[kwlug-disc] Advanced(?) Git usage question

Chris Frey cdfrey at foursquare.net
Mon Apr 21 21:03:04 EDT 2014

On Wed, Apr 16, 2014 at 11:33:23AM -0400, John Johnson wrote:
> This is definitely, not an an "advanced" question.
> But it is one for which I could not an answer on the Google or in Git tuts.
> Using Git, can one, without creating a branch, pull a file from an
> earlier commit?
> And (asking a lot) store the pulled file in a place other than the
> current working directory?

Yes.  If "give me file somepath/somefile.txt on branch broo" is what you
want, without anything else changing, you do:

	git cat-file blob broo:somepath/somefile.txt > /tmp/somefile.txt

If you do want to checkout a file into your workspace from another branch,
and commit it to this branch, you can do that with git-checkout:

	git checkout broo -- somepath/somefile.txt

This will leave your tree ready for a git-commit with the new copy of the
file.  The -- marker is just a separator between branch name and file name.

> Unless I need to do some more reading, git commands, revert pull and
> checkout all seem to operate on the repo as a whole and not on
> individual files.

To some degree.  Although with git-revert, you can ask it to not
commit automatically, using the -n switch, and then commit only the
changes you want to revert.  And with git-checkout, you can use
the -p option, which is like the inverse of git-add -p.  Use it to
select specific parts of a modified file to throw away.

- Chris

More information about the kwlug-disc mailing list