创建新分支时的基础分支是什么? [英] What is the base branch when a new one is created?

查看:157
本文介绍了创建新分支时的基础分支是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建分支时,我需要确认/纠正我的假设。如果我在master分支中,则执行以下操作:

I need to confirm/correct my assumptions when creating branches. If I'm in master branch, after doing:

git checkout -b some_branch

这意味着我已经从master开了一个新分支。

it means I have started a new branch from master.

如果我签出另一个分支,并在那里创建分支:

On the other hand, if I checkout another branch, and create a branch there:

git checkout some_branch
git checkout -b other_branch

这意味着我已经使用some_branch提交的所有当前代码创建了other_branch,对吗?

This means I've created other_branch using all the current code committed from some_branch, right?

并且,不管当前分支是什么,

And, regardless of the current branch, if this is done:

git branch branch_2 branch_1

然后将使用branch_1作为基础创建branch_2。
这些假设正确吗?

Then branch_2 will be created using branch_1 as the base. Are these assumptions correct?

推荐答案

在Git中没有这样的东西作为基本分支的分支。相反,只有一个当前提交,Git将该分支称为提示

There is no such thing in Git as a base branch of a branch. Instead, there is only a current commit, which Git calls the tip of the branch.

要从视觉上理解它,应该首先绘制Git的提交图(至少一部分)。这是一个只有三个提交的小型存储库的示例:

To understand this visually, as it were, you should start by drawing (at least part of) Git's commit graph. Here's an example of a tiny repository with just three commits in it:

A <-B <-C   <--master

任何给定提交的真实名称都是这些丑陋的哈希ID之一, code> c0ffeeface1deadbead ... 等。该哈希ID唯一地标识特定的提交,并且实际上是通过对该提交的内容进行哈希处理(因此称为哈希ID)而制成的。它们看起来是随机的,难以记住,所以在这里我只使用单个大写字母。

The "true name" of any given commit is one of those big ugly hash IDs, c0ffeeface1deadbead... and so on. This hash ID uniquely identifies the specific commit, and in fact is made by hashing (hence the name "hash ID") the contents of that commit. They look random and are impossible to remember, so here I just use single uppercase letters.

Git查看图形的方式是它从读取分支名称开始,例如 master 。该分支名称包含诸如 C 之类的提交的哈希ID 。我们说 master 指向提交 C

The way Git "sees" the graph is that it starts by reading a branch name, such as master. This branch name contains the hash ID of a commit like C. We say that master points to commit C.

同时,提交 C 本身包含其先前(或 parent )提交的哈希ID B 。所以我们说 C 指向 B ,就像 master 指向 C

Meanwhile, commit C itself contains the hash ID of its previous (or parent) commit B. So we say that C points to B, the same way master points to C.

同样,提交 B 指向 A A 是有史以来的第一次提交,因此没有地方可以指向...,所以它没有。我们将 A 称为 root 提交,它使我们(和Git)停止向后工作。

Likewise, commit B points back to A. A is the very first commit ever, so there's nowhere for it to point back to ... so it just doesn't. We call A a root commit, and it lets us (and Git) stop working backwards.

这些内部箭头有点烦人,请注意, B 的哈希ID实际上是 <$ c $的一部分c> C 本身,所以它永远都无法更改(如果我们尝试更改 C 的这一部分,我们会得到一个新的 提交)。因此,我们可以不再烦恼绘制它们,而改写:

These internal arrows are kind of annoying to draw, and note that the hash ID of B is actually part of C itself, so it can never change (if we try to change this part of C, we get a new, different commit). So we can stop bothering drawing them, and write instead:

A--B--C   <-- master

分支名称出来的箭头不是 常量,这就是 tip提交的整个概念的来源。

the arrow coming out of a branch name, though, is not constant, and that's where this whole idea of the tip commit comes from.

假设我们要向其中添加新的提交 master 。我们完成了Git所需的所有常规设置(添加或修改某些文件并使用 git add ),然后运行 git commit 。 Git:

Suppose we want to add a new commit to master. We do all the usual setup that Git requires (add or modify some files and use git add) and then run git commit. Git:


  • 编写新的提交 D (新的唯一哈希ID)。这个新的提交指向 C

  • Writes out a new commit D (which gets a new, unique hash ID). This new commit points back to C:

A--B--C     <-- master
       \
        D


  • 然后,更改 master (或更确切地说,是其存储的哈希ID),使其指向我们刚刚进行的新提交:

  • Then, changes master (or more precisely, its stored hash ID) so that it points to the new commit we just made:

    A--B--C
           \
            D   <-- master
    

    当然,没有理由再在图纸中保留这种纽结了:

    and of course there's no reason to keep this kink in the drawing anymore:

    A--B--C--D   <-- master
    


  • 这就是在Git中分支的增长方式。

    So this is how branches grow, in Git.

    要创建新的 分支,Git只是创建指向某个现有提交的分支名称​​

    To make a new branch, Git simply creates the branch name pointing to some existing commit:

    A
     \
      B
       \
        C
         \
          D   <-- master
    

    我们可以选择这些提交中的任何一个,并在那里指定新的分支名称。让我们选择 B 并在其中指向 newbr

    We can pick any one of these commits and make a new branch name point there. Let's pick B and make newbr point there:

    A
     \
      B   <-- newbr
       \
        C
         \
          D   <-- master
    

    我们可以使用 git branch newbr< thing- that-finds-B>

    我们如何找到 B ?好吧,一种方法是运行 git log 并剪切并粘贴哈希ID。但是另一种方法是使用分支名称。现在,名称 newbr 指向 B 。如果我们也想创建另一个分支点来提交 B

    How do we find B? Well, one way is to run git log and cut-and-paste the hash ID. But another way is to use a branch name. The name newbr now points to B. If we want to make another branch point to commit B too:

    git branch thirdbr newbr
    

    这使Git查找 newbr ,它指向 B ,并创建新名称 thirdbr ,它也指向 B

    This makes Git look up newbr, which points to B, and create the new name thirdbr, which ... also points to B:

    A--B   <-- newbr, thirdbr
        \
         C--D  <-- master
    

    这就是为什么创建Git中的一个分支是如此迅速地流血:它几乎什么都不做!

    This is why creating a branch in Git is so bleeping fast: it does almost nothing at all! It just makes a label that points to some existing commit.

    一些分支名称指向的提交称为该提交的 tip提交科。请注意,一次提交可以同时是多个分支的提示。这是有关Git的另一件事:一些提交同时在许多分支上。例如,提交 A (根提交)在每个分支上。 (虽然有点棘手,但在存储库中可能有多个root提交,我们现在不必担心这一点。到目前为止,您不能用所示的命令来这​​样做。)

    The commit to which some branch-name points is called the tip commit of that branch. Note that one commit can be the tip of several branches at the same time. This is part of a larger thing about Git: some commits are on many branches, all at the same time. For instance, commit A—the root commit—is on every branch. (It's possible to have more than one root commit in a repository, although it's a bit tricky, and we don't need to worry about that now. You can't do it with the commands shown this far.)

    分支标签的特殊属性是它们移动。他们不仅会移动,还会自动 移动。

    The special property of branch labels, though, is that they move. Not only do they move, they move automatically.

    我们在进行新的提交 D时已经看到了这一点。 Git将新提交的ID写入 master 。但是: Git如何知道使用 master 对此,Git如何知道使 D 的父母是 C

    We already saw this when we made new commit D. Git wrote the new commit's ID into master. But: How did Git know to use master? For that matter, how did Git know to make D's parent be C?

    当然,我们当时只有一个分支,但是现在我们有了三个标签 master newbr now >和 thirdbr 。不过,首先,让我们进行 git checkout thirdbr 并绘制结果:

    Well, of course we only had one branch at the time, but let's make a new commit now, now that we have three labels master, newbr, and thirdbr. First, though, let's do git checkout thirdbr, and draw the result:

    A--B   <-- newbr, thirdbr (HEAD)
        \
         C--D  <-- master
    

    图形中的内容并未真正改变, 1 ,只是我在此处添加了 HEAD 。 HEAD是Git如何知道 current 分支和提交中的哪个分支和提交。

    Nothing really changed in the drawing,1 except that I added the word HEAD here. HEAD is how Git knows which branch-and-commit are the current branch and commit.

    因此,我们现在通常进行修改-文件, git add git commit 。 Git会写出新提交,其父级设置为提交 B 。 Git发现 current 分支是 thirdbr thirdbr 指向 B ,因此当前的 commit B 。让我们绘制新的提交 E

    So now we do our usual modify-some-files, git add, and git commit. Git writes out the new commit, with its parent set to commit B. Git sees that the current branch is thirdbr and thirdbr points to B, so the current commit is B. Let's draw in the new commit E:

         E
        /
    A--B   <-- newbr
        \
         C--D  <-- master
    

    剩下的唯一事情就是移动当前分支 name thirdbr ,使其指向新分支提交 E

    The only thing left is to move the current branch name thirdbr so that it points to new commit E:

         E   <-- thirdbr (HEAD)
        /
    A--B   <-- newbr
        \
         C--D  <-- master
    

    我们都完成了:我们向分支 thirdbr 添加了新的提交(仍然是HEAD) ,因此仍然是当前分支,但现在 E 是当前提交)。

    and we're all done: we've added a new commit to branch thirdbr (which is still HEAD, and hence still the current branch, but now E is the current commit).

    当您在当前分支中添加一个提交,它是HEAD,它告诉当前提交是什么以及新提交的去向。 HEAD通常包含分支的名称,这就是 git checkout 会做:它签出特定的提交-通常是现有的尖端提交分支-然后设置文件 HEAD 来记住分支的名称。分支名称本身会记住 tip commit

    When you add a commit to the current branch, it's HEAD that tells what the current commit was and where the new commit goes. HEAD normally contains the name of a branch, and that's what git checkout does: it checks out a specific commit—usually, the tip commit of an existing branch—and then sets up the file HEAD to remember the name of the branch. It's the branch name itself that remembers the tip commit.

    使用 git checkout -b newname commit 的意思是:签出指定的 commit ,然后创建一个新的分支名称​​ newname 指向提交,然后将HEAD设置为该新名称。如果省略 commit 部分,则默认为使用HEAD。由于HEAD (始终)是当前的提交,因此Git跳过了检出部分,只是创建了新的分支名称并将其存储在 HEAD 文件。

    Using git checkout -b newname commit means: "check out the specified commit, then make a new branch name newname that points to that commit, and then set HEAD to that new name." If you leave out the commit part, the default is to use HEAD. Since HEAD is (always) the current commit, Git gets to skip the "check out" part and just create the new branch name and store it into the HEAD file.

    1 图中没有任何变化,Git确实必须更新我们的工作树和 index ,这样我们才能以提交 B

    1While nothing changed in the graph, Git did have to update our work-tree, and the index, so that we could have the files as of the way they were at commit B.

    这篇关于创建新分支时的基础分支是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆