Git Diff
What is git diff?
git diff shows the exact lines that changed in your files.
Use it to check your work before you stage or commit it:
git diff: changes you have not staged yetgit diff --staged: changes that are staged and will go into the next commitgit diff <commit> <commit>: changes between two commitsgit diff <branch>..<branch>: changes between two branches
See Unstaged Changes
Change a line in index.html, then run git diff:
Example: Show Unstaged Changes
git diff
diff --git a/index.html b/index.html
index 1be8d65..417ac8f 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,2 @@
-Hello World
+Hello Git
Welcome to Git
Try it Yourself »
How to Read the Output
--- a/index.htmland+++ b/index.html: the old and the new version of the file@@ -1,2 +1,2 @@: where the change is. 2 lines starting at line 1 in the old version, 2 lines starting at line 1 in the new- Lines starting with
-were removed - Lines starting with
+were added - Lines starting with a space did not change. They are shown for context
If the output is long, Git opens it in a pager. Use the arrow keys to scroll, and press q to quit.
See Staged Changes
After you stage a file with git add, plain git diff shows nothing for it. Use --staged (or its older name --cached) to see what will be committed:
Example: Show Staged Changes
git add index.html
git diff
git diff --staged
diff --git a/index.html b/index.html
index 1be8d65..417ac8f 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,2 @@
-Hello World
+Hello Git
Welcome to Git
Try it Yourself »
Only File Names (--stat and --name-only)
Use --stat to see which files changed and how many lines, without the full diff:
Example: Summary of Changes
git diff HEAD --stat
index.html | 2 +-
style.css | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
Try it Yourself »
Comparing with HEAD (the last commit) shows both staged and unstaged changes. Use --name-only to list just the file names:
Compare Two Commits
Give git diff two commits to see what changed between them. HEAD~1 means "the commit before the last one":
Example: Compare the Last Two Commits
git diff HEAD~1 HEAD
diff --git a/index.html b/index.html
index 1be8d65..9c9e0c6 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
Hello World
Welcome to Git
+Read the docs
Try it Yourself »
Compare Two Branches
Use two dots between branch names to see how one branch differs from another:
Example: Compare main and a Feature Branch
git diff main..new-title --stat
index.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Try it Yourself »
Ignore Spaces
Use -w to ignore changes that only add or remove spaces. This is useful when someone has re-indented a file: