Search code examples
gitgit-diff

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.

  • I'm actually stucked at what is actually "git diff HEAD".
  • And what is the difference between "git diff HEAD" and "git diff HEAD HEAD~1"

Solution

  • 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