工作区修改:新建b.txt,输入文字为test1,删除了a.txt
执行git add b.txt a.txt
$ git diff #无显示
$ git diff HEAD -- b.txt #工作区和分支的对比
diff --git a/b.txt b/b.txt
new file mode 100644
index 0000000..ad1a4f3
--- /dev/null
+++ b/b.txt
@@ -0,0 +1 @@
+text1
$ $ git diff --cached b.txt #暂存区和分支对比
diff --git a/b.txt b/b.txt
new file mode 100644
index 0000000..ad1a4f3
--- /dev/null
+++ b/b.txt
@@ -0,0 +1 @@
+text1
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: a.txt
new file: b.txt
更改b保存之后,git checkout -- b.txt
更改消失,b恢复原样,checkout命令清除了工作区
再次更改b保存,git diff
$ git diff
diff --git a/b.txt b/b.txt
index ad1a4f3..be69c1f 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1,2 @@
text1
+text2
\ No newline at end of file
接下来git add将b添加到暂存区,checkout之后再次git diff无返回内容,工作区中内容和暂存区中一样,用暂存区和分支对比发现新增内容(回到最近一次commit或者add时的状态,那么当再次修改之后,执行checkout就会回到上次add时的内容。)
在commit之前用git reset HEAD filename HEAD表示当前版本,把暂存区的修改撤销掉,再用git checkout -- filename 消除工作区