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?
The diff can be done with git diff (followed by the filename or nothing if you want to see the diff of all modified files). But if you already did something like git add * , you have to undo with git restore --staged . first.
You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch. Order does matter when you're comparing branches.
Comparing changes with git diff Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.
In answer to the original question, git diff isn't showing anything because you have a brand new directory, with a newly added file, but there are zero changes in the file for git diff to show. git status is showing that you added a new file, but git diff is for showing changes within files.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With