git status tells you which files changed. git diff shows what changed inside them, line by line.
Unstaged changes
With no arguments, git diff compares your working tree with the staging area:
$ git diff
diff --git a/greet.sh b/greet.sh
index 3b18e51..9f4d96d 100644
--- a/greet.sh
+++ b/greet.sh
@@ -1,2 +1,2 @@
#!/bin/sh
-echo "Hello"
+echo "Hello, team"Lines starting with - were removed and lines starting with + were added. Lines starting with a space are unchanged context. The @@ line says where in the file this block sits: starting at line 1, covering 2 lines, in both the old and new versions. Your hash values will differ.
Once you run git add greet.sh, plain git diff prints nothing for that file, because the working tree and staging area now match.
Staged changes
To see what your next commit will contain, compare the staging area with the last commit:
$ git diff --staged--cached is a synonym that you will also see. Checking this before git commit is a good habit.
Compare with commits and branches
Give a commit name to compare your working tree with it. HEAD means the commit you are currently on, so this shows staged and unstaged changes to tracked files together:
$ git diff HEADGive two names to compare them with each other:
$ git diff main featureThis shows how to get from main to feature. Swap the order and the plus and minus lines swap too.
Narrow the output
Add a path to limit the diff to one file or folder. Use --stat for a summary:
$ git diff --stat
greet.sh | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)New untracked files do not appear in git diff. Stage them first, then use git diff --staged.
Long output opens in a pager, usually less. Press q to leave it. The full option list is in the git diff documentation.
Try it
Edit a committed file and run git diff. Stage it, then compare the output of git diff, git diff --staged and git diff HEAD.
Check your understanding
git diff
All 3 sections · no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.