Uday Kiran
Uday Kiran

Reputation: 77

What is the difference b/w 'git diff HEAD' and 'git diff HEAD HEAD~1'?

I'm pretty new to git, can anyone please help me out.

Upvotes: 6

Views: 1748

Answers (3)

Sherman Chen
Sherman Chen

Reputation: 329

$ git diff HEAD differences between the latest commit and the working tree;

$ git diff HEAD HEAD~1 = $ git show differences between the latest commit and the commit before the latest.

(By the way,

$ git diff differences between the working tree and the index;

$ git diff --cached differences between the latest commit and the index.)

Upvotes: 0

Mathieu Rene
Mathieu Rene

Reputation: 994

git diff can take two revisions and show you the differences between them.

HEAD is a ref that points to the last commit of the current branch.

git diff HEAD will show you the changes between the last commit and what has not yet been committed (in contrast to git diff (with no revisions) which shows changes that have not been staged (using git add, so they can be committed afterwards)).

HEAD~1 is a special syntax allowing you to select the first parent of HEAD. Commits usually have a single parent (the previous commit), unless they merge two branches in which case they have a parent for the previous commit, and another for the merged branch.

Note that there is a shorthand for HEAD because it's used so often. You can replace it by @ with the same behaviour. git diff @ is the same as git diff HEAD, etc.

Upvotes: 8

ti7
ti7

Reputation: 18876

for the HEAD syntax

  • HEAD is the latest commit in a branch
  • HEAD~N is the nth older commit from HEAD

git diff HEAD will show you the difference between the current content and the most recent commit
this is especially helpful if you have staged content (git added, but not yet commited) and you are also interested in the unstaged difference git diff (which will show you only the difference between the staged commit state and changes on top of it)

git diff HEAD~1..HEAD will show you the difference between the most recent and its previous on the current branch, ignoring any current differences
this is useful for comparing ranges of previous commits (along with syntax like git reset --soft HEAD~N, which will bring the branch's HEAD to ~N discarding the intermediate commits, but leaving them staged as if you had used git add)

git diff HEAD..HEAD~1 shows you the reverse of HEAD~1..HEAD
this is usually a mistake and just a source of confusion

Upvotes: 3

Related Questions