~/git/practical-guide-better-understanding-git-diff.md

A Practical Guide for better understanding Git Diff

3 min readJuly 07, 2020by Gorav Singal#git
// TL;DR

Learn how to use git diff to compare changes between your working directory, staging area, and repository commits, including diffs for specific files and between commit IDs.

A Practical Guide for better understanding Git Diff

Introduction

In this guide, We will get basic understanding of various git diff options. We will see:

  • Diff between Local Working Dir and Staging area
  • Diff between Local Working Dir and Git repo (Latest commit)
  • Diff between Staging area and Git repo (Latest commit)

And,

  • Diff for particular file
  • Diff between commit ids

For other git training, visit:

Git diff

Diff between local working dir and staging area

If you do only git diff, it will show difference in files between staging and unstaged (local file).

Example, if you do changes in two files: t1, t2 And, do a git add t1 on one file only. Then, git diff will show difference for t2 only.

# after changing files t1, t2
$ git add t1
$ git status
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   t1

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   t2

Now, if I run git diff

$ git diff

diff --git a/t2 b/t2
index 45b983b..7c7e506 100644
--- a/t2
+++ b/t2
@@ -1 +1 @@
-hi
+hi hi hi

It shows difference of the file which has not been staged yet.

Diff between local working dir and Git repo(latest)

git diff HEAD

In the above example, I had modified two files: t1, t2. I staged only one file: t1.

$ git diff HEAD

diff --git a/t1 b/t1
index 45b983b..880d1a7 100644
--- a/t1
+++ b/t1
@@ -1 +1,3 @@
 hi
+hi
+hi
diff --git a/t2 b/t2
index 45b983b..7c7e506 100644
--- a/t2
+++ b/t2
@@ -1 +1 @@
-hi
+hi hi hi

Note, I get the diff of all the files on local dir vs the actual remote git repo.

Diff between Staging area and Git repo(latest)

In the above example, I had modified two files: t1, t2. I staged only one file: t1.

$ git diff --staged HEAD

diff --git a/t1 b/t1

index 45b983b..880d1a7 100644
--- a/t1
+++ b/t1
@@ -1 +1,3 @@
 hi
+hi
+hi

I’m only getting diff on the file I did staged.

Diff for only particular file

By default, git diff returns the diff of all the files changed/added. If you want to get diff of one particular file, do this:

git diff -- t2

Diff between Commit Ids

Lets list the commit ids in shorter format.

$ git log --oneline

e7dd2a0 (HEAD -> master, origin/master, origin/HEAD) t2
007675f t1
1a2af9c test
f997108 test
9a5a776 test
85587c6 test1
574c251 test 1

Lets get diff between two commits

$ git diff 574c251 e7dd2a0

## output

diff --git a/test1.txt b/t1
similarity index 100%
rename from test1.txt
rename to t1
diff --git a/t2 b/t2
new file mode 100644
index 0000000..45b983b
--- /dev/null
+++ b/t2
@@ -0,0 +1 @@
+hi
diff --git a/test2.txt b/test2.txt
new file mode 100644
index 0000000..45b983b
--- /dev/null
+++ b/test2.txt
@@ -0,0 +1 @@
+hi

Get the diff between a commit id and latest commit

git diff 574c251 HEAD

Get diff between latest and latest-1

git diff HEAD HEAD^

Diff between Branches

Assume you are having a branch: feature_branch, and you want to see the diff.

git diff master feature_branch

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

A Practical Guide in understanding Git Branch and Conflict resolution during merge

A Practical Guide in understanding Git Branch and Conflict resolution during merge

Introduction In this guide, We will learn about branching, handling conflict…

A Practical Guide on Understanding Git Best Practices

A Practical Guide on Understanding Git Best Practices

Introduction In this post, we will learn about some of Best practices while…

A Practical Guide on how to work with Git log command and history

A Practical Guide on how to work with Git log command and history

Introduction In this post, we will see ways to look at git history logs. For…

A Practical Guide on how to work with Git Basic Commands and workflows

A Practical Guide on how to work with Git Basic Commands and workflows

Introduction In this guide, we will see git basic commands, and fundamentals of…

A Practical Guide on how to to create your own git command alias

A Practical Guide on how to to create your own git command alias

Introduction In this guide, We will learn on how to create some handy command…

Git - How to create a Pull Request with no history of commits

Git - How to create a Pull Request with no history of commits

Introduction If you working on a github project in a team. Consider you have…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…