git diff usage

Git 中 diff 命令使用

整理总结工作中常用的git 命令, 今天是一个非常有用的命令 git diff

主要作用是用来比较差异,包括 commits之间,commit 和 工作区间 差异

命令格式如下:

1
2
3
4
5
git diff [<options>] [<commit>] [--] [<path>…​]
git diff [<options>] --cached [<commit>] [--] [<path>…​]
git diff [<options>] <commit> <commit> [--] [<path>…​]
git diff [<options>] <blob> <blob>
git diff [<options>] --no-index [--] <path> <path>

1. 比较工作区与暂存区

1
git diff 不加参数

2. 比较暂存区与 本地 最新版本 (最后一次commit)

1
git diff --cached

3. 比较工作区与最新本地版本库

1
git diff HEAD

4. 比较工作区与commit-id的差异

1
git diff commit-id

5. 比较暂存区与指定commit-id的差异

1
git diff --cached commit-id

6. 比较两个commit-id之间的差异

1
git diff commit-id commit-id

7. 比较不同本地分支的差异

1
git diff dev # 当前所处分支为master

8. 比较本地分支和远程分支之间的差异

1
2
git fetch origin master
git diff master origin/master

9. 比较差异只显示文件名

1
git diff --name-only

10 . 比较差异 显示更改文件状态

1
git diff --name-status
坚持原创技术分享,您的支持将鼓励我继续创作!