当前位置: 代码迷 >> 综合 >> git diff 用法
  详细解决方案

git diff 用法

热度:12   发布时间:2023-12-19 09:10:54.0

参考链接: 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

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

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

diff between {staging area, last commit}

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

diff between {last commit, working dir}

git diff --color <commitID>

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

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

查看哪些文件被 staged 了

# 获取当前的状态
git status .# 推荐使用下面的,简洁明了
git status -s
  相关解决方案