git diff between {working dir, staged area, last commit}

参考链接: http://xahlee.info/linux/git_diff.html

本文阐述 git diff 的不同用法。

git 中有三个主要区域

  • Working Directory 工作区。当前用户正在工作的区域
  • Staging Area(也被叫做 cache,index)。使用 git add 后存放的临时位置
  • HEAD 指向一个 commit 位置。通常情况下,是指上一次提交

上面的三个区域,都是在你本地的。每个 commit 都会有一个特定的标识(id),我们称之为 commit id。

如何查看 commit

1
2
# 显示最近的三次 commit
git log -3

diff between {working dir, staging area}

1
2
3
4
5
# diff working dir, staging area
git diff --color

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

diff between {staging area, last commit}

1
2
# diff satging area, last commit. 其中 staged 参数也可用 cached 替代
git diff --color --staged <commitID>

diff between {last commit, working dir}

1
git diff --color <commitID>

对同一分支的两个 commit 进行 diff

  1. git log file_name
  2. git diff commit_ID_1 commit_ID_2 file_name

查看哪些文件被 staged 了

1
2
3
4
5
# 获取当前的状态
git status .

# 推荐使用下面的,简洁明了
git status -s