zoukankan      html  css  js  c++  java
  • git学习笔记二-branch分支

    1.刚创建好的空仓库的分支是空的,即使是master分支也是不存在的。master分支是不能通过git branch 来创建的,只有在完成第一次提交才会自动创建,有git自动完成master分子的创建,也就是只有第一次提交创建好master分支后,才能再创建别的分支。因为git本质上就是基于图论原理的,图的第一个起点是系统在第一次提交的时候自动创建的,别的创建的所有的都是其他的分支都是第一个master分支后的“分支”。master作为主分支,在所有的git项目都是固定。所有的git项目都有master主分支,分分支则是不确定的。

    harvey@harvey-Virtual-Machine:~/demo3$ git init ../demo4 #创建一个新的空仓库
    Initialized empty Git repository in /home/harvey/demo4/.git/
    harvey@harvey-Virtual-Machine:~/demo3$ cd ../demo4
    harvey@harvey-Virtual-Machine:~/demo4$ echo "fsf">tes #创建新文件t
    harvey@harvey-Virtual-Machine:~/demo4$ git add ./ #添加到缓存区
    harvey@harvey-Virtual-Machine:~/demo4$ git branch#打印branch 列表发现为空,即使master分支也不存在
    harvey@harvey-Virtual-Machine:~/demo4$ git branch master#手动创建master分支失败
    fatal: Not a valid object name: 'master'.
    harvey@harvey-Virtual-Machine:~/demo4$ git branch 分支#手动创建"分支"分支失败
    fatal: Not a valid object name: 'master'.
    harvey@harvey-Virtual-Machine:~/demo4$ git commit ./#第一次提交commit(隐式的创建master分支)
    [master (root-commit) eca4197] sfsf:wq
     1 file changed, 1 insertion(+)
     create mode 100644 test
    harvey@harvey-Virtual-Machine:~/demo4$ git branch#打印master分支发现分支创建成功
    * master
    harvey@harvey-Virtual-Machine:~/demo4$ git branch 分支#再次创建“分支”分支
    harvey@harvey-Virtual-Machine:~/demo4$ git branch#再打印branch发现列表有数据了
    * master
      分支

    2.没有完成第一次提交也就是没有创建master分支的时候,是不能checkout master的因为没有master分支存在。这个时候,只能使用的是checkout . 命令,因为checkout .的意思就是把缓存区的数据覆盖工作空间.在没有创建提交的时候,可以用点代表全部的缓冲区文件,也可以用git checkout –<file>检出单独的文件。可以认为缓冲区stage就是也个目录,我们的checkout就是从stage这个目录把数据拷贝到当前工作的目录。而commit就是把stage这个文件夹再做单独的备份,备份从原理上也是在master这个主文件下,如果还是单个的子文件夹就是单独分支,如果在master后创建了新的分支就如同创建了新的文件夹。git其实也正是一个文件系统。

    harvey@harvey-Virtual-Machine:~$ rm -r -f demo4
    harvey@harvey-Virtual-Machine:~$ git init demo4
    Initialized empty Git repository in /home/harvey/demo4/.git/
    harvey@harvey-Virtual-Machine:~$ cd demo4
    harvey@harvey-Virtual-Machine:~/demo4$ echo "tttt">test
    harvey@harvey-Virtual-Machine:~/demo4$ git add ./
    harvey@harvey-Virtual-Machine:~/demo4$ git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    #
    #    new file:   test
    #
    harvey@harvey-Virtual-Machine:~/demo4$ rm test  #清空工作空间,这时候工作空间是空的
    harvey@harvey-Virtual-Machine:~/demo4$ git checkout master#checkou master失败
    error: pathspec 'master' did not match any file(s) known to git.
    harvey@harvey-Virtual-Machine:~/demo4$ git checkout . #checkout . 用工作空间的数据
    harvey@harvey-Virtual-Machine:~/demo4$ ls#发现从缓存位置取出了缓存的数据
    test

    3.基于我们认为stage是和工作空间相同的工作目录,历史提交也是工作空间,也就是文件夹。那么我们就可以用diff命令来比较的时候,本质上也是不叫的文件夹。

    harvey@harvey-Virtual-Machine:~$ rm -r -f demo4
    harvey@harvey-Virtual-Machine:~$ git init demo4
    Initialized empty Git repository in /home/harvey/demo4/.git/
    harvey@harvey-Virtual-Machine:~$ cd demo4
    harvey@harvey-Virtual-Machine:~/demo4$ mkdir a/a/
    mkdir: 无法创建目录"a/a/": 没有那个文件或目录
    harvey@harvey-Virtual-Machine:~/demo4$ mkdir a/a/ -p
    harvey@harvey-Virtual-Machine:~/demo4$ echo "test1">a.txt #创建第一级别的文本文件
    harvey@harvey-Virtual-Machine:~/demo4$ echo "test1">a/a/a.txt#创建文件夹下的文本文件
    harvey@harvey-Virtual-Machine:~/demo4$ git add ./
    harvey@harvey-Virtual-Machine:~/demo4$ git diff #提交在比较,因为此时两个文件夹是同步了的,所以diff没有输出内容,也就是两个文件夹没有差别
    harvey@harvey-Virtual-Machine:~/demo4$ echo "ttttt">>a.txt #更改文件
    harvey@harvey-Virtual-Machine:~/demo4$ echo "ttttt">>a/a/a.txt #更改文件
    harvey@harvey-Virtual-Machine:~/demo4$ git diff #发现输出的内容是比较的两个文件的差异,所以是比较的是文件夹
    diff --git a/a.txt b/a.txt
    index a5bce3f..9f42087 100644
    --- a/a.txt
    +++ b/a.txt
    @@ -1 +1,2 @@
     test1
    +ttttt
    diff --git a/a/a/a.txt b/a/a/a.txt
    index a5bce3f..9f42087 100644
    --- a/a/a/a.txt
    +++ b/a/a/a.txt
    @@ -1 +1,2 @@
     test1
    +ttttt
    
    #发现git diff的文件比较顺序是 ”diff stage 工作空间“
    harvey@harvey-Virtual-Machine:~/demo4$ git status -s #发现第一个A表示HEAD是空的,M表示stage和工作空间比较内容变化了,具体的变化内容只能通过diff来查看。也就是status显示的是摘要
    AM a.txt
    AM a/a/a.txt

  • 相关阅读:
    JavaScript学习笔记(六)——Map、Set与iterable
    JavaScript学习笔记(五)——条件判断与循环
    JavaScript学习笔记(四)——对象
    JavaScript学习笔记(三)——数组
    抽象代数 第三章 群
    进栈序列为(1,2,3..,n)有多少种出栈顺序
    Win10 快捷键
    主项定理Master Method
    算法导论笔记 第三十章 多项式与快速傅里叶变化
    算法导论笔记 第二十九章 线性规划
  • 原文地址:https://www.cnblogs.com/zhanghaiyublog/p/3660501.html
Copyright © 2011-2022 走看看