56

I created a repo, created a file inside it, put some content in the file, and committed the file. Now, I'd like to see a diff of that commit, which should ideally show the file that was added and the lines that were added to it.

However, git diff HEAD^ HEAD returns fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree., probably because this was the first commit to the repo.

How can this be resolved? Is there still a way to view a diff of the files that were added in the first commit?

Ali
  • 261,656
  • 265
  • 575
  • 769
  • 3
    `git show` can work. But the first diff is always from null to the whole content. – ElpieKay Nov 30 '16 at 08:50
  • @ElpieKay That works, but that also includes the commit summary. Possible to have it print only the diff without the commit summary If I do `git show `? – Ali Nov 30 '16 at 08:54
  • `git show --pretty=%% | sed 1,2d`. `%%` could be any placeholder which outputs only one line, e.g. `%h`, `%t`. – ElpieKay Dec 01 '16 at 12:40

4 Answers4

139

You can do:

git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 HEAD

4b825dc642cb6eb9a060e54bf8d69288fbee4904 is the id of the "empty tree" in Git and it's always available in every repository.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 7
    @CharlesBaley, Cool, Where do you get this sha-1 code? – gzh Nov 30 '16 at 09:29
  • Nice, is this something documented or is this relying on shaky functionality that may go away in future? – Ali Nov 30 '16 at 09:48
  • 9
    The id of the empty tree won't change while git continues to use sha1. You can use `$(printf '' | git hash-object -t tree --stdin)` for better readability. – CB Bailey Nov 30 '16 at 09:54
  • 9
    @gzh: I remember common sha1s. – CB Bailey Nov 30 '16 at 09:55
  • What does this sha1 stand for? – Ali Nov 30 '16 at 10:03
  • 21
    This is hilarious to me. Why not add a named flag for this? Something like `git diff --empty-tree head`. Similar to how we have `--root` for interactive rebase. – Daniel Waltrip Jun 24 '17 at 18:21
  • See also [Is git's semi-secret empty tree object reliable, and why is there not a symbolic name for it?](https://stackoverflow.com/questions/9765453/is-gits-semi-secret-empty-tree-object-reliable-and-why-is-there-not-a-symbolic) – user202729 Sep 12 '18 at 10:28
  • 1
    @DanielWaltrip maybe this is new, but `git diff-tree` now has a `--root` option that does exactly what you were asking about. – larsks Jul 02 '19 at 21:01
  • 1
    Git supports SHA256 now, so you can't always rely on this hash id. `git init --object-format=sha256 && git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 HEAD` returns an error – Corey Sep 20 '22 at 23:02
  • @CBBailey Sadly, `git init --object-format=sha256 && git diff $(printf '' | git hash-object -t tree --stdin) HEAD` fails -- any suggestions? – Josiah Yoder Dec 08 '22 at 20:55
4

Now that Git has experimental support for SHA256 and a transition plan for migrating the hash function from SHA1 to SHA256, you can no longer rely on a hash constant for the empty tree. Instead, it's best to dynamically retrieve it based on whatever hash function your repository is using:

git diff $(git hash-object -t tree /dev/null)
Corey
  • 351
  • 3
  • 4
1

Maybe try with:

git log -p -n 1
tfe
  • 60
  • 2
  • 1
    This doesn't show the first commit. It shows the most recent one. It's not an answer to the given question. – ErikE Apr 09 '23 at 03:50
0

you can try:

git show <first-commit-sha>

or if you only have 1 commit you can simply use:

git show HEAD

Aya Salama
  • 1,458
  • 13
  • 17