views:

79

answers:

3

The problem is the following: how to fetch a few files from a previous commit in git?

I could get the files one by one and replace them by their old version:

git show <commit>:<path>  >!  path

However, I would like to know whether there is a different way of performing the same thing, possibly with git reset.

In fact, I gather that

git reset <commit> <path>

puts the old file into the index. But how can the files in the index be moved back to the working tree? Or is this a bad approach?

PS: There is a wonderful graphical explanation that makes everything clear: http://marklodato.github.com/visual-git-guide/.

+1  A: 

Have you had a look at the "git checkout" command? E.g.

git checkout master~2 Makefile

checks out the file (Makefile) from the older commit, which is what I understood you want to do. You'll then can decide if you want do add it to the index using "git add".

simon
+1  A: 

It's easier with git checkout HEAD~1 -- $FILENAME to checkout the version previous to your last commit, replace HEAD~1 by a rev-ID or a tag for other revisions.


edit there actually is git-checkout-index, so you could use git checkout-index $FILENAME.


edit2 you may need to use the -f switch, i.e. git checkout -f -- $FILENAME. The -- tells git you mean a file (only necessary if you have a branch or tag of the same name).

Tobias Kienzler
`git checkout <path>` (with no revision specified) checks out from the index anyway. `checkout-index` is more of a plumbing (lower-level) command.
Jefromi
@Jeromi: thanks, I somehow forgot that...
Tobias Kienzler
@Tobias: Even though checkout-index is lower level, it's good to know about it. Thanks!
EOL
+4  A: 

To put a file from a given revision into both the index and the working tree in one step, check it out:

git checkout <commit> <path>

If you've already put it only into the index, using git reset <commit> <path>, then no need to specify a commit:

git checkout <path>

That is, by default, git checkout <path> copies from index to work tree, but if provided a revision argument, it will copy from there into the index first.

Jefromi