git diff
— Inspecting and Comparing Changes in Git
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!
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>
Tip
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 if you want to better understand how to read the actual diff output.
- More frequently asked questions about Git & version control