简体   繁体   中英

View deleted file before git commit

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]$

Do as instructed:

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

should do what you want.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM