2011年1月28日星期五

Git recover a deleted file

$ echo "hello" > afoo
$ git add afoo
$ git commit -m "add afoo"

$ rm afoo # we delete afoo

$ git checkout afoo # we recover deleted afoo

If you modify afoo

$ echo "world" > afoo
$ git checkout a # afoo will be reset to HEAD

$ echo "world" > afoo
$ git add affo
$ git commit -m "world"

$ git checkout HEAD^ afoo # afoo will be reset to last version as "hello"

=========================

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.

======================================

$ git checkout HEAD . # if you want all files reverted.


没有评论:

发表评论