Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to diff a commit with its parent

Tags:

git

git-diff

Aside from writing an alias or script, is there a shorter command for getting the diff for a particular commit?

git diff 15dc8^..15dc8 

If you only give the single commit id git diff 15dc8, it diffs that commit against HEAD.

like image 253
Brian L Avatar asked Jan 12 '09 18:01

Brian L


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 .

What is the parent of a git commit?

Git "First Parent" meaning When you use the git merge command to merge a branch/commit into your current branch, it joins their development histories together in a new commit. This new commit records both as its parents; the branch/commit you're merging in and the branch you are on when the merge occurs.

What is the command to output the differences between the most recent commit and its grandparent?

The git diff command shows the differences between the files in two commits or between your current repository and a previous commit.


2 Answers

Use git show $COMMIT. It'll show you the log message for the commit, and the diff of that particular commit.

like image 99
mipadi Avatar answered Oct 01 '22 10:10

mipadi


Use:

git diff 15dc8^! 

as described in the following fragment of git-rev-parse(1) man page (or in modern Git gitrevisions(7) man page):

Two other shorthands for naming a set that is formed by a commit and its parent commits exist. The r1^@ notation means all parents of r1. r1^! includes commit r1 but excludes all of its parents.

This means that you can use 15dc8^! as a shorthand for 15dc8^..15dc8 anywhere in Git where revisions are needed. For the diff command, the git diff 15dc8^..15dc8 is understood as git diff 15dc8^ 15dc8, which means the difference between parent of commit (15dc8^) and commit (15dc8).

Note: the description in git-rev-parse(1) man page talks about revision ranges, where it needs to work also for merge commits, with more than one parent. Then r1^! is "r1 --not r1^@" i.e. "r1 ^r1^1 ^r1^2 ..."


Also, you can use git show COMMIT to get the commit description and diff for a commit. If you want only the diff, you can use git diff-tree -p COMMIT.

like image 35
4 revs, 3 users 60% Avatar answered Oct 01 '22 12:10

4 revs, 3 users 60%