Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 277
Ali Avatar asked Nov 30 '16 08:11

Ali


People also ask

How do I see diff before 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.

How do I see git diff?

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.

What is git diff command?

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.

Why is git diff not showing?

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.


1 Answers

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.

like image 76
CB Bailey Avatar answered Sep 22 '22 20:09

CB Bailey