跟着廖雪峰的git教程学习(2)时光机穿梭(三)管理修改

现在,假定你已经完全掌握了暂存区的概念。下面,我们要讨论的就是,为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件。

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

为什么说Git管理的是修改,而不是文件呢?我们还是做实验。第一步,对hello.html做一个修改,比如加一行内容:

<!DOCTYPE html>
<html>
<head>
	<title>hello</title>
</head>
<body>
   hello world! and i love myself~ you know?
   and I love my parents too.
</body>
</html>

然后,添加:

$ git add hello.html

zuo@zuo-PC MINGW32 /d/mygit (master)
$ git status
On branch master
Your branch is ahead of 'BigPauZ/master' by 1 commit.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)


        modified:   hello.html

然后,再修改hello.html:

<!DOCTYPE html>
<html>
<head>
	<title>hello</title>
</head>
<body>
   hello world!  I love myself~ you know?
   and I love my parents too.
   I wish I love the people love me too~ 
</body>
</html>

提交:

$ git commit -m "i love the people love me too"
[master 5368cc6] i love the people love me too
 1 file changed, 1 insertion(+)

提交后,再看看状态:

$ git status
On branch master
Your branch is ahead of 'BigPauZ/master' by 2 commits.
  (use "git push" to publish your local commits)
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:   hello.html

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 -- hello.html命令可以查看工作区和版本库里面最新版本的区别:

$ git diff HEAD -- hello.html
diff --git a/hello.html b/hello.html
index 27a5b65..be5fc4b 100644
--- a/hello.html
+++ b/hello.html
@@ -1,10 +1,11 @@
-<<!DOCTYPE html>
+<!DOCTYPE html>
 <html>
 <head>
        <title>hello</title>
 </head>
 <body>
-   hello world! and i love myself~ you know?
+   hello world!  I love myself~ you know?
    and I love my parents too.
+   I wish I love the people love me too~
 </body>
 </html>
\ No newline at end of file

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

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

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

好,现在,把第二次修改提交了,然后开始小结。

小结一下:

现在,我又理解了Git是如何跟踪修改的,每次修改,如果不add到暂存区,那就不会加入到commit中。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值