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?
3 Answers
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.
-
8Note that
path/to/file
is full path from the top of project (top dir of repository). Sep 8, 2009 at 21:47 -
3I've got
fatal: Invalid object name 'HEAD^'.
(I have to mention, that I have only "Initial commit")– vladkrasOct 26, 2016 at 12:37 -
1If 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 Mar 23, 2017 at 16:21
-
4I 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. Sep 11, 2019 at 22:09
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
.
-
2You 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)– DrorApr 17, 2014 at 20:56
-
1Nice 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.– pedorroJan 9, 2015 at 19:59
-
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).– NickolaiApr 26, 2019 at 18:38 -
As of the date of this comment, using
^
does not work withgit show
orgit log
. Had to usegit log -p
with~
to get this to work.– tamatoNov 24, 2021 at 21:24
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>
-
5On 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. Sep 24, 2013 at 18:59