tags:

views:

8077

answers:

4

Given the following interaction:

$ git add foo 
$ git commit -m "Initial import of 'foo'"
$ rm foo # This could be any destructive action on foo, like editing it.

How do I restore 'foo' in my working copy? I'm looking for something like:

$ git <magic> foo
Restored foo to revision <blah>.
+15  A: 
git checkout foo

That will reset foo to HEAD. You can also:

git checkout HEAD^ foo

for one revision back, etc.

Greg Hewgill
+8  A: 

@Greg Hewgill

Amusingly, 'git checkout foo' will not work if the working copy is in a directory named foo; however, both 'git checkout HEAD foo' and 'git checkout ./foo' will:

$ pwd
/Users/aaron/Documents/work/foo
$ git checkout foo
D   foo
Already on "foo"
$ git checkout ./foo
$ git checkout HEAD foo
Aaron Maenpaa
or `git checkout -- foo`
knittl
+4  A: 

@zacherates Isn't that because you have a branch named foo in that repository? Recent versions of Git would tell you about the ambiguity. In the meantime, using ./foo is probably the easiest way to resolve it.

Note, however, that git checkout ./foo and git checkout HEAD ./foo are not exactly the same thing; case in point:

$ echo A > foo
$ git add foo
$ git commit -m 'A' foo
Created commit a1f085f: A
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 foo
$ echo B >> foo
$ git add foo
$ echo C >> foo
$ cat foo
A
B
C
$ git checkout ./foo
$ cat foo
A
B
$ git checkout HEAD ./foo
$ cat foo
A

(The second add stages the file in the index, but it does not get committed.)

Git checkout ./foo means revert path ./foo from the index; adding HEAD instructs Git to revert that path in the index to its HEAD revision before doing so.

Damien Diederen
A: 

It seems that

for i in `git checkout`; do git checkout $i; done

is the easy (mega-dirty) answer if you want all files reverted.

qubodup
git checkout HEAD .
wRAR