git原理之二:branch分支

1. 前言

前面我们提到,commit object通过parent串成一条树链,该链允许有多个分叉。

假设链结构图如下:

整个链有三个分叉:

  • A,B,F,G构成一个分叉
  • A,B,C,D,E构成一个分叉
  • A,B,H,I,J构成一个分叉

每一个分叉叫做branch

2. branch的表示

分叉是一条完整的链,在git中怎么表示分叉呢?

方法很简单,用分叉中的最后一个commit object,来代表整个分叉

由于commit之间通过parent指针链在一起,只需要指定最后一个commit, 就能通过parent指针把整个分叉找出来。

下图中,只要我们知道commit G就能顺着指针把整个链回溯出来

3. branch的名字

为了方便管理,还需要给branch指定名字。我们最常见的branch名字是什么?没错,就是master,此外大家还常见的dev, debug,test等等,都是branch的名字

在一个空的项目中,.git/refs目录下没有任何内容

$ tree .git/refs

.git/refs
├── heads
└── tags

在上一节中,我们通过

 echo 024000dad0c2baedccc57a39d8f195abbde93be0 >  .git/refs/heads/master

写入了一个branch的名字,叫做master,因此目录为:

$ tree .git/refs
.git/refs
├── heads
│ └── master
└── tags

即在git/refs/heads下创建了一个master文件,master文件的内容是最后一个commit object id

让我们通过常规操作来看得更清楚,这次我们用指令来创建一个新的branch

git branch newbranch master

可以看到,heads目录下生成了newbranch文件

$ tree .git/refs/
.git/refs/
├── heads
│ ├── master
│ └── newbranch
└── tags

来看一下newbranch文件的内容:

$ cat .git/refs/heads/newbranch

024000dad0c2baedccc57a39d8f195abbde93be0

显然,和master一摸一样,只有一行数据,记录了最后一个commit的id

我们可以给任意分叉指定任意的名字,甚至一个分叉多个名字。这些名字都保存在.git/refs/heads目录下,加上名字后,commit链的结构如图:

3. HEAD

在执行提交操作的时候,需要指定是提交到哪个分支。也可以提交到当前默认分支

当前默认分支的内容保存在.git/HEAD文件中:

$ cat .git/HEAD

ref: refs/heads/master

可以看到,里面记录着当前默认分支的名称

我们切换下分支看看:

$ git checkout newbranch

$ cat .git/HEAD
ref: refs/heads/newbranch

引入HEAD后,整个链的结构为:

4. reference

HEAD和branch名字都叫做reference,因为他们仅仅简单的记录了commit object的id或者分支名称,用于从逻辑上描述分支,或者当前分支。性质上和前面提到的各种object完全不同

Ref

  • https://medium.com/hackernoon/understanding-git-branching-2662f5882f9

索引

~~~异次元传送门



《 “git原理之二:branch分支” 》 有 4 条评论

  1. can you buy generic It should be acknowledged, however, that a general instrument such as QHES might have shortcomings for the assessment of cost effectiveness analyses Appendix

  2. Importantly, none of the 120 patients developed cardiac tamponade or progressed to pericardial constriction priligy reddit

  3. Published 28 03 2019 Received 15 08 2018 can you buy priligy

回复 get generic cytotec online 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

About Me

一位程序员,会弹吉他,喜欢读诗。
有一颗感恩的心,一位美丽的妻子,两个可爱的女儿
mail: geraldlee0825@gmail.com
github: https://github.com/lisuxiaoqi
medium: https://medium.com/@geraldlee0825