How to Show Changes in Git Commit

Ankit Raj Goyal Feb 02, 2024
  1. Use git diff <commit_ref>^! to Show Changes in Commit in Git
  2. Use git diff <commit_ref>~ <commit_ref> to Show Changes in Commit in Git
  3. Use git diff <commit_ref>^ <commit_ref> to Show Changes in Commit in Git
  4. Use File Scoping Option to Show Commit Changes Only in a Specific File/File Type in Git
  5. a Quick Alternate Method - Use git show Command With Options to Show Changes in Commit in Git
  6. Use Alias With git show for a Quick Shortcut to Show Changes in Commit in Git
  7. Show the Most Recent Commit Changes With git diff HEAD^ HEAD in Git
  8. Show Changes in a Few Commits Back in Git
How to Show Changes in Git Commit

You need to quickly show commit changes in Git for a fast workflow. We use git diff and git show with various options to help you show the exact commit changes you need.

We toggle with gitrevisions options to find shortcuts for common use cases to Git show changes in the commit.

Use git diff <commit_ref>^! to Show Changes in Commit in Git

This is a neat, crisp method to quickly show changes in a particular commit. It uses the gitrevisions <rev>^! shortcut to pack all the find functionality in a single, short line of code.

git diff <commit_ref>^!

The <commit_ref>^! short-hand notation refers to the commit <commit_ref> but excludes all its ancestors.

In essence, this means it shows only the commit <commit_ref>. The git diff command then shows only the changes made by this commit.

commit ref short hand

Use git diff <commit_ref>~ <commit_ref> to Show Changes in Commit in Git

The gitrevisions range <commit_ref>~..<commit_ref> means commits that we can reach from <commit_ref> but not from its ancestors.

So, it means only the <commit_ref> and none of its ancestors.

In git diff, it is the same as git diff <commit_ref>~ <commit_ref> and shows the changes between the <commit_ref> and its immediate parent.

git diff <commit_ref>~ <commit_ref>

Thus, this shows changes made in only the <commit_ref> commit.

tilda method commit commit

Use git diff <commit_ref>^ <commit_ref> to Show Changes in Commit in Git

The <commit-ref>^ refers to the commit’s first parent.

git diff <commit_ref>^ <commit_ref>

It shows changes between <commit_ref> and its first parent. This is the same as the changes made only in the commit <commit_ref>.

caret method commit commit

Use File Scoping Option to Show Commit Changes Only in a Specific File/File Type in Git

We can pass a file scoping option to all methods above to show only the changes in a particular file or file type.

git diff <commit_ref>^! <file|file type>

file scoping option commit changes

a Quick Alternate Method - Use git show Command With Options to Show Changes in Commit in Git

We can use the git show command with clever options to show only the differences made by a commit.

Note
git diff is a dedicated command for showing commit changes and is the recommended method. The git show is meant to show many details of a commit, not only the differences.

We use it as a quick shortcut here.

git show --color --pretty=format:%b <commot_ref>

If we pass these options, the git show only shows commit changes.

Pro Tip: This method also works with the Root Commit or the First Commit in the Work Tree. The git diff fails with the Root Commit as the Root Commit has no ancestors.

git show with options for differences

git diff fails with root commit

We see that git diff fails with the Root Commit, but git show works.

Use Alias With git show for a Quick Shortcut to Show Changes in Commit in Git

We see above that we need to pass many options to git show to show only the differences in a commit.

We can create a shortcut by setting an alias for that long command in the ~/.gitconfig file.

nano ~/.gitconfig

[aliases]
od = show --color --pretty=format:%b

add alias in gitconfig

We add the alias od or Only Difference. We can then use it as a shortcut.

git od <commit_ref>

use alias to show differences

We can use gitrevisions suffices likes ^ and ~ in clever ways to write quick shortcuts to show recent commit changes.

Show the Most Recent Commit Changes With git diff HEAD^ HEAD in Git

HEAD refers to the latest commit.

git diff HEAD^ HEAD

It shows the difference between the HEAD or the most recent commit and its parent.

This means it shows only its changes (the latest commit).

most recent commit shortcut

Show Changes in a Few Commits Back in Git

You can use the ~ gitrevisions suffix to hop back to the n-th ancestor of HEAD and show changes in that commit.

git diff HEAD~2 HEAD~1

It will show the differences between the 2nd-ancestor and 1st-ancestor of the HEAD or most recent commit.

It will thus show the changes in the 1st-ancestor commit or the second most recent commit.

few commits back changes

Related Article - Git Commit