了解了暂存区的概念,我们进一步理解Git跟踪并管理的是修改,而非文件
你会问,什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改
举例说明
1、对readme.txt进行修改,比如增加一行
Git is a distributed version control system
Git is free sofware distributed under the GPL
Git tracks changes
2、将1中的修改添加到暂存区
$ git add README.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README.txt
3、再修改readme.txt,增加两个单词,注意并没有再一次将修改提交到暂存区
Git is a distributed version control system
Git is free sofware distributed under the GPL
Git tracks changes of file
4、如果想查看文件的具体内容,可以使用cat 文件名
命令
$ cat README.txt
Git is a distributed version control system
Git is free sofware distributed under the GPL
Git tracks changes of file
5、执行一次git commit
命令提交到版本库,并查git status
查看当前工作区的状态
$ git commit -m "git tracks changes"
[master 0304e0b] git tracks changes
1 file changed, 2 insertions(+), 1 deletion(-)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.txt
no changes added to commit (use "git add" and/or "git commit -a")
咦,怎么第二次的修改没有被提交?
别激动,我们回顾一下操作过程:
第一次修改 -> git add -> 第二次修改 -> git commit
你看,我们前面讲了,Git管理的是修改,当你用git add命令后,在工作区的第一次修改被放入暂存区,准备提交,但是,在工作区的第二次修改并没有放入暂存区,所以,git commit只负责把暂存区的修改提交了,也就是第一次的修改被提交了,第二次的修改不会被提交。
提交后,用git diff HEAD -- readme.txt
命令可以查看工作区和版本库里面最新版本的区别
$ git diff HEAD -- README.txt
diff --git a/README.txt b/README.txt
index bbc5954..10177ad 100644
--- a/README.txt
+++ b/README.txt
@@ -1,3 +1,3 @@
Git is a distributed version control system
Git is free sofware distributed under the GPL
-Git tracks changes
\ No newline at end of file
+Git tracks changes of file
\ No newline at end of file
可见第二次的修改并没有提交,那怎么提交第二次修改呢?你可以继续git add
再git commit
,也可以别着急提交第一次修改,先git add
第二次修改,再git commit
,就相当于把两次修改合并后一块提交了:第一次修改 -> git add -> 第二次修改 -> git add -> git commit
附件
小结
现在,你又理解了Git是如何跟踪修改的,每次修改,如果不add到暂存区,那就不会加入到commit中