Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorthand for getting the diff from the last N commits?

Tags:

I know I can do:

git diff HEAD^..HEAD 

But is there some shorthand that's easier to remember, like:

git diff foo N 

where N can be any number of commits from now to get a cumulative diff of?

like image 324
joachim Avatar asked Feb 18 '11 07:02

joachim


People also ask

How can I see the diff of a commit?

To see the diff for a particular COMMIT hash, where COMMIT is the hash of the commit: git diff COMMIT~ COMMIT will show you the difference between that COMMIT 's ancestor and the COMMIT .

How do I compare a previous commit?

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.

How do I see changes after last commit?

To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

Which command shows the changes between commits?

"git diff" always show the difference between two commits (or commit and working directory, etc.).


2 Answers

From the SPECIFYING REVISIONS of the git rev-parse man page:

A suffix ~<n> to a revision parameter means the commit object that is the <n>th generation grand-parent of the named commit object, following only the first parent.
I.e. rev~3 is equivalent to rev^^^ which is equivalent to rev^1^1^1.

Consider the examples in the git diff man page:

git diff HEAD^..HEAD git diff HEAD^.. git diff HEAD^ HEAD 

are equivalent forms (thanks chrisk for the HEAD^.. form, as mentioned in the comments).
(they are not equivalent to git diff HEAD^, as Mark Longair comments, since it diff with the working directory, not the last commit)

So:

git diff HEAD~15       # diff the working tree with the 15th previous commit git diff HEAD~15 HEAD  # diff the last commit  with the 15th previous commit 

should do what you need (as khmarbaise mentions in the comment).

like image 94
VonC Avatar answered Sep 22 '22 21:09

VonC


Use git diff HEAD~N. Or use git diff HEAD~N.. to exclude uncommitted changes.

like image 23
Zaz Avatar answered Sep 22 '22 21:09

Zaz