开始使用Git

新增、初始Repository

$ cd /tmp
$ mkdir git-practice
$ cd git-practice
$ git init

上面三条指令都是系统自带的指令,只有最后一条是属于git的,实际上它的作用就是在当前目录创建一个.git文件夹,这个文件夹就是git的精髓了,后面再讲这个文件夹里面的内容

把文件交给Git管控

工作区,暂存区,存储库

通过上面的内容已经知道使用git的过程种会遇到这三个区域了,这三个区域的关系和联系就这么简单

202212261501

工作区,暂存区,存储库

查看记录

使用 git log 就可以查看之前的commit了

$ git log
commit ed0fe3b7753ba49d0923d85100786bdba693ef1d (HEAD -> master)
Author: lol <233@qq.com>
Date:   Mon Dec 26 23:26:59 2022 +0800

    add c.vim

commit aaa37137a002593fc26ffe1a5366ed4a54db2c67
Author: lol <233@qq.com>
Date:   Mon Dec 26 22:47:35 2022 +0800

    b

commit 2bcd045a50439781c124e33ce9b7caea0bf5c8c3
Author: lol <233@qq.com>
Date:   Mon Dec 26 22:47:16 2022 +0800

    add a.vim

越新的commit在越上面,通过输出的信息,大致可以知道三个信息

那堆乱码其实是通过SHA-1(Secure Hash Algorithm 1)计算来的,计算方式在后面会讲,它的作用类似于每个commit的身份证,因为碰撞率很低

如果在git log 后面加上参数,可以看到不同的结果 git log --online --graph

$ git log --oneline
ed0fe3b (HEAD -> master) add c.vim
aaa3713 b
2bcd045 add a.vim
$ git log --graph
* commit ed0fe3b7753ba49d0923d85100786bdba693ef1d (HEAD -> master)
| Author: lol <233@qq.com>
| Date:   Mon Dec 26 23:26:59 2022 +0800
|
|     add c.vim
|
* commit aaa37137a002593fc26ffe1a5366ed4a54db2c67
| Author: lol <233@qq.com>
| Date:   Mon Dec 26 22:47:35 2022 +0800
|
|     b
|
* commit 2bcd045a50439781c124e33ce9b7caea0bf5c8c3
  Author: lol <233@qq.com>
  Date:   Mon Dec 26 22:47:16 2022 +0800

      add a.vim
$ git log --oneline --graph
* ed0fe3b (HEAD -> master) add c.vim
* aaa3713 b
* 2bcd045 add a.vim

和在命令行里查看提交历史比起来,在图形界面里看提交历史其实更方便一点,但功能都是相似的

Git查询历史记录时的常见问题

在Git中删除文件或者变更文件名

在Git中,无论是删除文件还是变更文件名的本质都是一样的,都是“改动”

删除文件

我们可以直接手动删除,然后我们看一下git status

$ rm a.vim
$ 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:    a.vim

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

可以看到a.vim的状态是deleted,如果你想要删除就可以git add a.vim

$ git add a.vim
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    a.vim

现在这个改动被添加到暂存区了,只需要commit就行了

你也可以让Git帮你删除 git rm a.vim

$ git rm a.vim
rm 'a.vim'
[paradox@windows git]$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    a.vim

相当于帮你做好了删除和 git add 这两步,后面还是需要 commit

无论是 rm 还是 git rm 都会真的把这个文件从工作目录中删除,如果你不算真的想把这个文件删除,只是不想让Git再跟踪这个文件了,可以使用 git rm a.vim --cached

$ git rm a.vim --cached
rm 'a.vim'
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    a.vim

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

$ ls
a.vim  c.vim

我们可以看到,只是从Git上删除了a.vim,但是它还是存在在我们的工作区里

变更文件名

$ mv c.vim d.vim #把c.vim改名成d.vim
$ 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:    c.vim

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

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

我们可以看到,git把这步操作看成两步,删除了c.vim再添加了一个d.vim

然后我们来 git add 一下

$ git add d.vim
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   d.vim

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:    c.vim

$ git add c.vim
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    c.vim -> d.vim

发现把d.vim和c.vim都 git add 了,git就识别出是 renamed 了,这是因为文件的内容没有改变,git判断出这只是单纯的改名

如果我们再往d.vim里添加一些内容

$ echo 6 > d.vim
$ git add d.vim
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    c.vim
        new file:   d.vim

git并没有识别成 renamed 再加上 modified

你也可以让git帮你改名 git mv c.vim d.vim

$ git mv c.vim d.vim
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    c.vim -> d.vim

事实上,文件的名称并不重要,Git其实是根据文件的内容来计算SHA-1的值的,重要的是文件的内容。当更改文件名的时候,Git并不会为此做出一个新的Blob对象,只是指向原来的Blob对象,但是因为文件名变了,所以Git会为此做出一个新的Tree对象,后面会解释Git对象是干什么用的

虽然书上是这么说的,但是我的本地环境下却会改变,有可能是git版本不一样了,待我探索一番,虽然这个变化没什么太大的关系

经过我的一番探索,发现这个SHA-1的值还和commit的时间有关系,我们继续往后探索,看看那个计算公式是什么样的