Git时光机穿梭笔记2

一.工作区和暂存区
  工作区:就是我们在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区,工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库,Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD
    用git add把文件添加进去,实际上就是把文件修改添加到暂存区
    用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支
    创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,git commit就是往master分支上提交更改。 可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改
  
  再次实践: 先修改readmetet文件,再在工作区新增一个LICENSE文本文件,用git status 查看状态

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   readme.tet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        LINCENSE

no changes added to commit (use "git add" and/or "git commit -a")
​


    Git非常清楚地告诉我们,readme.txt被修改了,而LICENSE还从来没有被添加过,所以它的状态是Untracked
  用两次命令git add,把readme.txt和LICENSE都添加后,用git status再查看一下

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   LINCENSE
        modified:   readme.tet

此时的暂存区

再次证实git add命令实际上就是把要提交的所有修改放到暂存区(Stage),然后,执行git commit就可以一次性把暂存区的所有修改提交到分支

一旦提交后,没有对工作区做任何修改,那么工作区就是“干净”的

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ git commit -m"understand how stage work"
[master e86cefa] understand how stage work
 2 files changed, 2 insertions(+)
 create mode 100644 LINCENSE

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

 

 二.管理修改

为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件

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

实验验证

第一步,对readme.txt做一个修改,比如加一行内容,再add,之后查看状态

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ vi readme.tet

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ git add readme.tet

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   readme.tet

然后,再修改readme.txt:
再次提交:
提交后,再看看状态:

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ vi readme.tet

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ cat readme.tet
Git is a good software
Git is very good software in the world
Git has a mutable index called stage
Git tracks changes of files

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ git commit -m"git tracks changes"
[master 448c9a8] git tracks changes
 1 file changed, 1 insertion(+)

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   readme.tet

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

凌风@▒▒▒plus MINGW64 /d/GitWarehouse/learngit (master)
$ git diff HEAD -- readme.tet
diff --git a/readme.tet b/readme.tet
index 1c84466..b9ea9da 100644
--- a/readme.tet
+++ b/readme.tet
@@ -1,4 +1,4 @@
 Git is a good software
 Git is very good software in the world
 Git has a mutable index called stage
-Git tracks changes
+Git tracks changes of files

如何把第二次的修改提交

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

小结
明确Git是如何跟踪修改的,每次修改,如果不用git add到暂存区,那就不会加入到commit中

三.撤销修改 

当我们不小心写了一些不该写的东西的时候,怎么撤销修改呢?

比如:

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ cat readme.tet
Git is a good software
Git is a very good software
Git has a mutable index called stage
Git tracks changes of file
My boss is so fool

1、我们可以直接手动删除最后一句

2、用git status 查看一下

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   readme.tet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        learngit/

no changes added to commit (use "git add" and/or "git commit -a")

可以发现,Git告诉我们,git restore <file>可以丢弃工作区的修改

 git restore <file>意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:
1、readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
2、readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态
总之,就是让这个文件回到最近一次git commit或git add时的状态

此时再次查看readme.tet文件,会发现已经复原

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ cat readme.tet
Git is a good software
Git is a very good software
Git has a mutable index called stage
Git tracks changes of files

当我们不仅不小心写了些不该写的,还不小心add到暂存区了呢?

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ git add readme.tet

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ cat readme.tet
Git is a good software
Git is a very good software
Git has a mutable index called stage
Git tracks changes of files
My boss is so fool

幸好此时还没有commit,先用git status查看一下状态 

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   readme.tet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        learngit/

Git同样告诉我们,用命令git restore --staged <file>可以把暂存区的修改撤销掉(unstage),重新放回工作区:

撤销掉暂存区的修改后,我们再次git status查看

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   readme.tet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        learngit/

no changes added to commit (use "git add" and/or "git commit -a")

现在暂存区是干净的,工作区有修改,

我们接上一步,再撤销工作区的,用git restore <file>, 

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ git restore readme.tet

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ cat readme.tet
Git is a good software
Git is a very good software
Git has a mutable index called stage
Git tracks changes of files

假设我们不但改错了东西,还从暂存区提交到了版本库,怎么办呢?还记得版本回退一节吗?可以回退到上一个版本

不过,这是有条件的,就是你还没有把自己的本地版本库推送到远程。Git是分布式版本控制系统,我们后面会讲到远程版本库,一旦把fool boss提交推送到远程版本库,就真的惨了

小结
又到了小结时间。
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git restore <file>。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git restore --staged <file>,就回到了场景1,第二步按场景1操作。
场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。 

四.删除文件

在Git中,删除也是一个修改操作
先添加一个新文件test.txt到Git并且提交

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ git add test.txt
warning: in the working copy of 'test.txt', LF will be replaced by CRLF the next time Git touches it

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ git commit -m"add test.txt"
[master c8a038f] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

通常直接在文件管理器中把没用的文件删了,或者用rm命令删了$ rm test.txt

此时Git知道我们删除了文件,因此,工作区和版本库就不一致了

用git status查看

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    test.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        learngit/

no changes added to commit (use "git add" and/or "git commit -a")

现在有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit,此时文件就从版本库完全删除了

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ git rm test.txt
rm 'test.txt'

凌风@▒▒▒plus MINGW64 /d/GitWarehouse (master)
$ git commit -m"remove test.txt"
[master 4541db5] remove test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt

为什么此处要commit,原因很简单,git跟踪的是修改,需要提交git rm这个修改后,才是完全从版本库删除了,先手动删除文件,然后使用git rm <file>和git add<file>效果是一样的

如果是删错了呢?

因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本

用git restore test.txt

git restore其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”注意:从来没有被添加到版本库就被删除的文件,是无法恢复的!

小结
命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值