How to get Git diff of the first commit?

Click Upvote picture Click Upvote · Nov 30, 2016 · Viewed 11.5k times · Source

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?

Answer

CB Bailey picture CB Bailey · Nov 30, 2016

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.