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:

Working Directory
Files in your project's directory.
Staging Area (aka cache, index)
A virtual area (a index file) that git add is placed into.
HEAD
A reference to a specific commit (think of it as a variable). Normally, it points to the last commit in local repository. (that is, after you did git commit). [see Git: What's HEAD]

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.

get commit ID

git log -9
show the last 9 commit's commit id.

diff between {working dir vs staging area}

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

diff between {staging area vs last commit}

# diff staging area vs last commit
git diff --color --staged ‹commitID›

diff between {last commit vs working dir}

First, use git log to get a commit ID.

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

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.

find what files are changed

git status
Show state of {last commit, staging area, working dir}

example

git status sample 2021-01-31
git status sample 2021-01-31

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

git