Git:git diff命令详解,比较两个版本的差异
Lasted 2020-01-01 21:46:30
git diff命令用于比较两个版本之间的差异。
语法
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>
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. 查看当前文件的差异。
git diff
效果同上,不显示-a的内容
git diff HEAD
diff --git a/README.md b/README.md index e845566..e965047 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -README +Hello
Note:如果是新加的文件,不会显示任何信息,只比较修改的文件。
2. 显示简短信息。
git diff --stat
main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
3. 查看某个版本到当前版本的差异。
先使用git log --oneline -5
命令查看提交的历史记录,获取短哈希值,从下面信息查看显示添加了一个文件。
git log --oneline -5
4d83e02 note 04f275c 2
git diff 04f275c 4d83e02
diff --git a/main.c b/main.c new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/main.c @@ -0,0 +1 @@ +main
4. 查看两个分支的差异。
git diff branch1 branch2
5. 只显示有差异文件的文件名。
git diff --name-only
6. 打包两个版本增量文件。
git diff 0a03487 4d83e02 --name-only | xargs zip docsxyz.zip
adding: README.md (stored 0%) adding: main.c (stored 0%)
7. 比较暂存区和某个版本的差异。
git diff --cached 0a03487
diff --git a/README.md b/README.md index e845566..e965047 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -README +Hello diff --git a/main.c b/main.c new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/main.c @@ -0,0 +1 @@ +main