Git: Diff Between {Working Dir, Staged Area, Last Commit}

By Xah Lee. Date: . Last updated: .

Here's how to diff between various areas of git.

There are 3 major concepts of places:

All are local on your disk.

One important concept is Commit ID. Every commit has a ID. The commit id is a 40 digits hexadecimal, for example: 3b6ea398cc2d69212b04c29f06b8d15c0af34e34.

How to get commit ID?

#  to show the last 3 commit's commit id
git log -3

How to diff between {working dir, staging area}?

# diff working dir, staging area
git diff --color
# diff working dir, staging area, 1 file
git diff --color filename

How to diff between {staging area, last commit}?

# diff staging area, last commit. (--staged is same as --cached)
git diff --color --staged ‹commitID›

How to diff between {last commit, working dir}?

First, use git log to get a commit ID.

# diff last commit, working dir
git diff --color ‹commitID›

How to diff between 2 commits in the same branch?

First do

git log file_name

to find the commits IDs. Then, do

git diff commit_ID_1 commit_ID_2 file_name

git diff 3d5cf 5aa95 myfilename

You only need to type the first few characters of commit id.

How to find what files are changed?

git status → get summary of {last commit, staging area, working dir}

# show changes between {staging area, last commit} and {staging area, working dir}
git status .

example

◆ git status .
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   ../../ergoemacs_org/emacs/blog.html
#       modified:   ../../ergoemacs_org/emacs/blog.xml
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   ../../ergoemacs_org/emacs/elisp_eval_lisp_code.html
#       modified:   ../../ergoemacs_org/emacs/elisp_function_eval_and_doc_lookup.html
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       ../../ergoemacs_org/emacs/i/emacs_describe-function.png
#       ../js/earth_globe_JavaScript.js

The first section “# Changes to be committed:” is the diff between {staging area, last commit}.

The second section “# Changes not staged for commit:” is the diff between {working dir, staging area}.

The 3rd section “# Untracked files:” is also the diff between {working dir, staging area}.

2014-01-18 Thanks to Nick Alcock for review.

If you have a question, put $5 at patreon and message me.

git

  1. Install git
  2. Git Basics
  3. Git Ignore File
  4. Commit Files
  5. Push to Server
  6. Pull from Server
  7. Find Difference
  8. View Log
  9. Revert Change
  10. Branching
  11. Temp Save: git stash
  12. What's HEAD
  13. FAQ