What is the difference between "git show test.rb" and "git show HEAD:test.rb"?

310 views Asked by At

Could you explain the difference between git show test.rb and git show HEAD:test.rb?

The command git show HEAD:test.rb returns:

test file contents

while git show test.rb returns:

commit a8e90b3dbf4eed03cdbb3cd3b99f98e9153c7219 
Author: Misha Moroshko <[email protected]> 
Date:   Thu Oct 27 17:03:04 2011
+1100

    asd

diff --git a/test.rb b/test.rb new file mode 100644 index
0000000..b48e119
--- /dev/null
+++ b/test.rb @@ -0,0 +1 @@
+test file contents
2

There are 2 answers

2
manojlds On BEST ANSWER

git show for commits will show the log message and textual diff. So that is what you get when you do git show, with the commit being assumed to be HEAD. And git show file shows the log message and textual diff for HEAD, filtered to file.

To show the content of the files at a particular commit, you do git show commit:file. So the git show HEAD:file shows the contents of the file in HEAD.

From gitrevisions man page:

A suffix : followed by a path (e.g. HEAD:README); this names the blob or tree at the given path in the tree-ish object named by the part before the colon. :path (with an empty part before the colon, e.g. :README) is a special case of the syntax described next: content recorded in the index at the given path.

Also refer to the examples in the git show manual ( git show --help)

1
Lily Ballard On

git show test.rb can be rewritten as git show -- test.rb. This form makes it more obvious that you're calling git show and filtering the output to just test.rb. Since git show defaults to showing HEAD, this is the same as git show HEAD -- test.rb. Basically, it will show the commit info, but only give you a diff for the specific file test.rb.

However, git show HEAD:test.rb is explicitly instructing git show that the object you want to show is the blob that lives at the path test.rb reachable from the tree associated with the HEAD commit.