1

How do I view a file using git which has been deleted but not yet committed? Note that I do not wish to restore it, just view it.

[Michael@devserver test]$ git status # On branch master # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: file_delete # modified: file_edit # no changes added to commit (use "git add" and/or "git commit -a") [Michael@devserver test]$ git diff file_edit diff --git a/file_edit b/file_edit index c8c019f..c9287d1 100644 --- a/file_edit +++ b/file_edit @@ -1,2 +1,2 @@ Hello -Goodby +later [Michael@devserver test]$ git diff file_delete fatal: ambiguous argument 'file_delete': unknown revision or path not in the working tree. Use '--' to separate paths from revisions [Michael@devserver test]$ 
3
  • Git probably assumes that if you delete the file, you don't want to see it anymore. Why do you want to see this file? Commented Feb 24, 2017 at 14:34
  • @TimBiegeleisen: Do you suppose git should also assume if you've edited a file you don't want to see the old state of that file? The entire point of git is to preserve history, why shouldn't they be able to see the file? Perhaps they want to validate their changes - including deletes Commented Feb 24, 2017 at 14:37
  • @MarkAdelsberger I agree with your answer. Commented Feb 24, 2017 at 14:42

2 Answers 2

5

The message isn't as clear as it might be, but it does hint at the solution:

git diff -- file_delete 

The -- tells git that all remaining arguments are pathspecs, even though they might not match anything in the working tree. Honestly I think git is being a little weird here, but to try to explain...

In general if you have

git diff xyzzy 

without context you don't know whether xyzzy is a branch (or tag) that should be compared against the entire worktree, or a filename for which the worktree should be compared with HEAD.

If there is a branch named xyzzy and no file named xyzzy in the worktree, then git assumes you mean to compare that branch with the worktree.

If there is a file named xyzzy in the worktree and no branch named xyzzy, then git assumes you mean to compare the HEAD version of that file to what's in the worktree.

If there is a branch named xyzzy and there is a file named xyzzy in the worktree, then git says this is ambiguous and wants you to clarify. You clarify by putting a -- before any filenames (or other pathspecs).

The slightly weird case: If there is no branch named xyzzy and there is also no file named xyzzy in the worktree, then git says this is ambiguous. The reason I think this is weird is, git could err on the side of assuming you mean a file and it would usually be what you meant, especially if at least in HEAD there is such a file. But git doesn't want to make that assumption unless the file is actually in the worktree. So it says this is ambiguous.

And again if it's ambiguous, you clarify by putting a -- before any filenames / pathspecs.

In general the git diff documentation (https://git-scm.com/docs/git-diff) tells us about the -- separator. I don't really recall if/where it calls out this specific nuance, though. My best advice is to pay attention to git's error output; it's more explicit than most programs about what it thinks you might need to do to fix a problem, and even though this message may not quite get its point across it does correctly anticipate what you needed to do.

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

2 Comments

Thanks Mark, While it works, I don't understand why. Can you give a better explanation on what this does? Also, do you know where git's man explains this? Thanks
Added some explanation; hope this helps
3

Do as instructed:

Use '--' to separate paths from revisions 
git diff -- file_delete 

should do what you want.

4 Comments

I've been looking at man git, and I cannot see where this is documented. Also, what do you mean by "revisions". I searched the man, and this word is not in there.
The quote is from the very error message you posted in your question. ;-) "revisions" refers anything that could identify a commit: A branch name, a tag name, a remote branch name, the respective revision's SHA1 hash, ...
For git diff something, Git interprets something as a file name or path if that file or path exists on the file system. If not, it assumes you must have meant a revision. (Which can look the same as paths, as branch names are allowed to contain slashes.) If you want Git to interpret it as a path name (e.g. because it exists in the repository history, although not on the working copy, as is the case here with the deleted file) you can indicate that intend to Git by putting all paths after the --.
To get detailed synopsis of Git subcommands, use the help subcommand. For this case, that'd be git help diff. (Alternatively, man git-diff shows the same content. Note the hyphen (-).)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.