Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-diff

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"
like image 600
Uday Kiran Avatar asked Jul 10 '21 14:07

Uday Kiran


People also ask

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

The git diff command uses the above git data sources as its input and executes the diffing function on them. The git diff HEAD [filename] command allows you to compare the file version in your working directory with the file version last committed in your remote repository.

What is git A and B diff?

What Are a and b in git diff? The first line of the output from the previous example was diff --git a/file. txt b/file. txt. The a and b in the line are what we call “prefixes.” They indicate the source and origin for the comparison.

What does git diff mean?

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.

How do I diff two versions in git?

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.


Video Answer


2 Answers

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.

like image 54
Mathieu Rene Avatar answered Oct 19 '22 17:10

Mathieu Rene


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

like image 23
ti7 Avatar answered Oct 19 '22 16:10

ti7