Mohammad Karmi
Mohammad Karmi

Reputation: 1465

What is the difference between git diff and git diff HEAD?

I stumbled while reading the following at atlassian : https://www.atlassian.com/git/tutorials/saving-changes/git-diff

git diff HEAD ./path/to/file

This example is scoped to ./path/to/file when invoked, it will compare the specific changes in the working directory, against the index, showing the changes that are not staged yet. By default git diff will execute the comparison against HEAD. Omitting HEAD in the example above git diff ./path/to/file has the same effect.

I figured out that there are cases they will not be the same , if the file I have is staged then no changes for git diff. but for git diff HEAD it will compare with the head instead of index. so is the sentence below wrong ?

By default git diff will execute the comparison against HEAD

because it compares with the index

Upvotes: 1

Views: 104

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521979

My read of your question is that the example given in the documentation is assuming that the stage is empty, and, in particular, no version of the file in question has been staged versus what is in the working directory.

Under these assumptions, the HEAD option which appears after git diff would be unnecessary, because in this case omitting HEAD would result in the stage being used. And both of these should be the same, so HEAD therefore becomes optional, at least in this example.

Upvotes: 2

Related Questions