Git: Find Difference Between {Working Dir, Staged Area, Last Commit}, or 2 Commits

, , …,

This page is a git tutorial for finding changes/differences in last commit or various areas of git.

For git basics, see: git Tutorial.

There are 3 major concepts of places:

When you do diff, you are diff between two of the above places.

One important concept is Commit ID. Every commit has a ID. The commit id is a 40 digits hex ⁖ 3b6ea398cc2d69212b04c29f06b8d15c0af34e34.

Type git log to get a list of recent commit IDs.

git status: summary of {local last commit, staging area, working dir}

How to find what files are changed?

git status . will report changes between {staging area, local last commit} and {working dir, staging area}, for the current dir and all subdirs.

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

here's a sample output of git status .:

◆ 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, local 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}.

git diff Between {Working Dir, Staged Area, Local Last Commit}

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

# diff working dir, staging area
git diff --color

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

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

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

First, use git log to get a commit ID.

# diff local last commit, working dir
git diff --color commit ID

Git Diff Between 2 Commits

How to diff between 2 versions in the same branch?

First do a git log file name to find the version names. Then, do git diff commit ID 1 commit ID 2 file name. Example:

git diff 3d5cf 5aa95 ergoemacs_org/emacs/whitespace-mode.html

Set Git Diff to Always Use Color

How to make git always use color in diff?

git config --global color.ui true

thanks to Nick Alcock for help.

blog comments powered by Disqus