Frequently asked questions around Git and Version Control.
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 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:
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.