Search code examples
gitgit-show

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


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 <michael.moroshko@gmail.com> 
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

Solution

  • 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)