首页 技术 正文
技术 2022年11月10日
0 收藏 735 点赞 2,182 浏览 1867 个字

  假如你已经阅读了https://www.cnblogs.com/cxq0017/p/9663452.html Git工作区和暂存区,并且已经掌握了暂存区的概念,下面我们要讨论的是,为什么Git比其他版本系统设计得优秀,因为Git跟踪并管理的是修改,而非文件。

  你会问,什么是修改?比如你新增了一行,这就是一个修改,删除了一行,更改了某些字符,也是一个修改,,删了一些由加了一些,也是一个修改,甚至创建了一个新文件,也算一个修改。

  为什么说Git管理的是修改,而不是文件呢?我们还是做实验来验证。原本readme.txt的内容如下:

Git is a distributed version control system
Git is free sofwore distributed under the GPL
Git has a mutable index called stage.

现在第一步,我们对readme.txt做一个修改,比如加一行内容:

Git is a distributed version control system
Git is free sofwore distributed under the GPL
Git has a mutable index called stage.
Git tracks changes.

然后,添加:

$ 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

Git is a distributed version control system
Git is free sofwore distributed under the GPL
Git has a mutable index called stage.
Git tracks changes of files

提交

$ git commit -m “git tracks changes”
[master a4376f5] 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 47cca0b..4fbbe0a 100644
— a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,4 @@
Git is a distributed version control system
Git is free sofwore distributed under the GPL
Git has a mutable index called stage.
-Git tracks changes.
\ No newline at end of file
+Git tracks changes of file
\ No newline at end of file

可见,第二次修改确实没有被提交。

那怎么提交第二次修改呢?你可以继续git addgit commit,也可以别着急提交第一次修改,先git add第二次修改,再git commit,就相当于把两次修改合并后一块提交了:

第一次修改 -> git add -> 第二次修改 -> git add -> git commit

好,现在,把第二次修改提交了.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298