Restoring Removed Files With Git

By Andrew Price, 2009-01-11 04:01:49 in General.

So you've accidentally rm'd a file in a git tree and you want it back. What to do?

$ git checkout HEAD -- path/to/removed/file

This tip was brought to you by Andy's determination to remember how to do it next time, in cooperation with wasted moments spent reading the wrong man page (git reset). Sigh.


RainCT writes:

That's why I love bzr :P.

2009-01-11 12:39:35

Robin writes:

HEAD is the default, you can just do it like this:

git checkout path/to/removed/file

At first I was annoyed that the command is not "git revert" as with Subversion. But now that I'm used to Git, I think that "git checkout" is right because it is consistent with how Git works (you can checkout out all files of a branch, or single files of a branch, and it defaults to the current branch).

2009-01-11 15:55:51