简体   繁体   中英

How to get Git diff of the first commit?

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?

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.

也许尝试:

git log -p -n 1

you can try:

git show <first-commit-sha>

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

git show HEAD

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)

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