git diff — Inspecting and Comparing Changes in Git
Short answer
git diff shows the line-by-line differences between two states of your repository, with lines prefixed by + (added) and - (removed). Running git diff with no arguments shows unstaged changes — the difference between your working directory and the index (staging area). To see only staged changes (what will be included in the next commit), use git diff --cached or git diff --staged. To compare two specific commits, run git diff <commit1> <commit2>; to compare two branches, use git diff <branch1>..<branch2>. Narrow the output to a specific file by appending its path: git diff HEAD -- path/to/file, and add --stat to get a summary of changed files and line counts instead of the full diff.
The git diff command helps you see, compare, and understand changes in your project. You can use it in many different situations, e.g. to look at current changes in your working copy, past changes in commits, or even to compare branches.
In this short article, we'll talk about the most important use cases you need in your daily work.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Free. We email you a ZIP with our cheat sheets.
What was changed since I last committed?
Running the plain git diff command without any parameters can be pretty helpful: it will show you all of your local changes since you last committed.
git diff
What changes did I make in a certain file?
Often, you'll want to see only the changes in a certain file. You can simply add the path of a file as an option:
$ git diff index.html
diff --git a/index.html b/index.html
index f42e433..59c866e 100644
--- a/index.html
+++ b/index.html
@@ -1,8 +1,7 @@
<ul>
<li>blue item</li>
<li>red item</li>
- <li>green item</li>
- <li>purple item</li>
+ <li>orange item</li>
</ul>
What changes did I already add to the Staging Area?
By adding the --staged (or alternatively: --cached) option, Git will show which local changes you have already added to Staging Area, via "git add":
# Staged changes in a certain file...
$ git diff --staged index.html
# Staged changes in all local files...
$ git diff --staged
Can I see the changes in a more concise way?
Another helpful option is --color-words. Instead of the "classic" display mode in diffs, where old and new contents are displayed in separate lines, this option shows a more concise view. Here's an example:
$ git diff --color-words index.html
diff --git a/index.html b/index.html
index f42e433..59c866e 100644
--- a/index.html
+++ b/index.html
@@ -1,8 +1,7 @@
<ul>
<li>blue item</li>
<li>red item</li>
<li>green item</li>
<li>purple<li>orange item</li>
</ul>
No Command Line Needed
Showing Diffs in Tower
In case you are using the Tower Git GUI, its internal diff viewer comes with inline highlighting, whitespace toggling, the ability to show the complete file - and also the possibility to see diffs for image files!
How can I compare two branches?
It's also possible to compare two branches to each other. This helps you find out how exactly the code in both branches is different:
git diff main feature/login
Tip: you might also see notations where the two branches are separated by ".." (e.g. "git diff main..feature/login"). This produces the same output as separating the branches with a space.
How can I compare a certain file in two different branches?
Sometimes, you might want to compare how a certain file differs in two branches. You can do this simply by adding the file's path:
git diff main feature/login index.html
This will show you how the file "index.html" was changed in the feature/login branch, compared to how it looks in the main branch.
Learn More
- Check out the chapter on Inspecting Changes with Diffs – Desktop GUI edition if you want to better understand how to read the actual diff output.