136

I've deleted a file with Git and then committed, so the file is no longer in my working copy. I want to look at the contents of that file, but not actually restore it. How can I do this?

4 Answers 4

184
git show HEAD^:path/to/file 

You can use an explicit commit identifier or HEAD~n to see older versions or if there has been more than one commit since you deleted it.

Sign up to request clarification or add additional context in comments.

4 Comments

Note that path/to/file is full path from the top of project (top dir of repository).
I've got fatal: Invalid object name 'HEAD^'. (I have to mention, that I have only "Initial commit")
If the deletion is staged but not committed, use HEAD instead of HEAD^ (since it existed in HEAD). For instance, I thought I needed file, so I added it and committed to save my work, then later deleted it when I came up with a different solution. To see the original file before committing again, I did git show HEAD:path/to/file
I think it's also worth mentioning that this only works if the file was deleted in the most recent commit. If the file was deleted several commits ago, you'll need to use one of the other answers.
73

If this is a file you've deleted a while back and don't want to hunt for a revision, you can use (the file is named foo in this example; you can use a full path):

git show $(git rev-list --max-count=1 --all -- foo)^:foo 

The rev-list invocation looks for all the revisions of foo but only lists one. Since rev-list lists in reverse chronological order, then what it lists is the last revision that changed foo, which would be the commit that deleted foo. (This is based on the assumption that git does not allow a deleted file to be changed and yet remain deleted.) You cannot just use the revision that rev-list returns as-is because foo no longer exists there. You have to ask for the one just before it which contains the last revision of the file, hence the ^ in git show.

4 Comments

You might need to change the last foo to ./foo. And for them not using bash, get the id with "git rev-list --max-count=1 --all -- foo" and then do "git show 5824127a8d99576632a04ac2b5c2344bcf751967:./foo" with the id (524.. is the id)
Nice answer. I had to use ~ instead of ^. Not sure why. And just so people are clear on it, 'foo' here must be a full path from the git root.
You can write **/foo if you don't know the path (this will work for the rev-list command but not the show command. However with the commit from the rev-list command you can then find the path).
As of the date of this comment, using ^ does not work with git show or git log. Had to use git log -p with ~ to get this to work.
11

Since you might not recall the exact path, you can instead get the sha1 from git log then you can simply issue

 git cat-file -p <sha1> 

1 Comment

On behalf of Samuel Slund: One way to find the sha1 sum to use above is: git whatchanged --no-abbrev that gives output similar to git (or svn) log.
6

Here is a way to find a file that was deleted long ago, even if you don't remember the exact name and/or path:

git log --stat=1000 --full-history -- "**/*partial_file_name*.*"

  • --stat=1000 lists all the files in the commits. The 1000 ensures you'll see the entire relative path to the deleted file (https://stackoverflow.com/a/10460154/99717)
  • --full-history shows the reverse commit history for the file, starting with the delete commit.

This way, you can also search for a specific file version in a specific commit by scanning the messages and history.

With the commit hash, you can view the file using:

git show COMMIT_HASH:entire/relative/path/to/deleted_file_name.ext

Getting the relative path right is important, else git will tell you it can't find that path. Also, you have to use a commit hash before the actual file delete commit, which will have nothing, because git show shows you the version of the file in that commit.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.