管理修改
GIt 比其他版本控制系统设计得优秀,因为 Git 跟踪并管理的是修改,而非文件。
验证:
-
对 readme.txt 做一个修改;
$ cat readme.txt Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes
-
然后,执行 git add;
$ git add readme.txt $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: readme.txt #
-
再修改 readme.txt;
$ cat readme.txt Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files.
-
执行 git commit 提交;
$ git commit -m "git tracks changes" [master 519219b] git tracks changes 1 file changed, 1 insertion(+)
-
再查看状态;
$ 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 add:把工作区的修改被放入暂存区;
- git commit:只负责把暂存区的修改提交;
所以,第一次修改提交了,第二次修改并未被提交。
命令查看 git 工作区和版本库的区别
执行 git diff HEAD – readme.txt 可以查看工作区和版本库的区别
$ git diff HEAD -- readme.txt
diff --git a/readme.txt b/readme.txt
index 76d770f..a9c5755 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,4 @@
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
-Git tracks changes.
+Git tracks changes of files.