Git 命令 - git diff

Git 命令 - git diff

1. git diff - 查看更改前后的差别

git diff 命令可以查看工作树、暂存区、最新提交之间的差别。

在刚刚提交的 README.md 中写点东西。

# Git Tutorial

1.1 查看工作树和暂存区的差别

执行 git diff 命令,查看当前工作树与暂存区的差别。

$ git diff
strong@foreverstrong:~/github_work/git-tutorial$ git diff
diff --git a/README.md b/README.md
index e69de29..f6cfe9a 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+# Git Tutorial
strong@foreverstrong:~/github_work/git-tutorial$ 

由于我们尚未用 git add 命令向暂存区添加任何东西,所以程序只会显示工作树与最新提交状态之间的差别。

“+” 号标出的是新添加的行,被删除的行则用 “-” 号标出。我们可以看到,这次只添加了一行。
用 git add 命令将 README.md 文件加入暂存区。

$ git add README.md
strong@foreverstrong:~/github_work/git-tutorial$ git add README.md 

1.2 查看工作树和最新提交的差别

如果现在执行 git diff 命令,由于工作树和暂存区的状态并无差别,结果什么都不会显示。要查看与最新提交的差别,请执行以下命令。

$ git diff
$ git diff HEAD
strong@foreverstrong:~/github_work/git-tutorial$ git diff
strong@foreverstrong:~/github_work/git-tutorial$ 
strong@foreverstrong:~/github_work/git-tutorial$ git diff HEAD
diff --git a/README.md b/README.md
index e69de29..f6cfe9a 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+# Git Tutorial
strong@foreverstrong:~/github_work/git-tutorial$ 

在执行 git commit 命令之前先执行 git diff HEAD 命令,查看本次提交与上次提交之间有什么差别,等确认完毕后再进行提交。这里的 HEAD 是指向当前分支中最新一次提交的指针。

由于我们刚刚确认过两个提交之间的差别,所以直接运行 git commit 命令。

$ git commit -m "Add index"
strong@foreverstrong:~/github_work/git-tutorial$ git commit -m "Add index"
[master 47b9787] Add index
 1 file changed, 1 insertion(+)
strong@foreverstrong:~/github_work/git-tutorial$ 

保险起见,我们查看一下提交日志,确认提交是否成功。

$ git log
strong@foreverstrong:~/github_work/git-tutorial$ git log
commit 47b9787d5032b8d8ed6e9e4c7df8474f728a35e8
Author: chengyq116 <chengyq116@163.com>
Date:   Sun Dec 17 20:04:06 2017 +0800

    Add index

commit 3f44bb613b0da5e24655c3ce69cb62e3befe5ae6
Author: chengyq116 <chengyq116@163.com>
Date:   Sun Dec 17 18:51:14 2017 +0800

    First commit
strong@foreverstrong:~/github_work/git-tutorial$ 

成功查到了第二个提交。

References

https://yongqiang.blog.csdn.net/

(日) 大塚弘记 著, 支鹏浩, 刘斌 译. GitHub入门与实践[M]. 北京:人民邮电出版社, 2015. 1-255

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值