简体   繁体   English

git分支总是要合并吗?

[英]Are git branch always to be merged?

Before : My question is focused on Git philopsophy and best practice, not on technical POV. 之前:我的问题重点是Git哲学和最佳实践,而不是技术POV。

I wonder if a Git branch fate is to be eventually merged all the time . 我不知道Git分支的命运是否最终会一直被合并 Will we stick to best practice using a branch to live it's own life without merging at any time to master, and knowing it at start? 我们是否会坚持使用最佳实践来使用分支机构过自己的生活,而不会在任何时候合并并掌握它,并且一开始就不了解它?

Would it be better in this case to create an new repo with this branch content? 在这种情况下,用此分支内容创建一个新的仓库会更好吗?

A branch name is mostly just a name for a commit, just like a tag name. 分支名称主要只是提交的名称,就像标记名称一样。 There's no need to use it to make merge commits, though there's no compelling reason not to use it that way either. 无需使用它进行合并提交,尽管也没有令人信服的理由也不使用这种方式。

When a name—including a branch name or a tag name—identifies some commit, such as the commit whose hash ID starts with 1234567... , we say that this name points to that particular commit. 当名称(包括分支名称或标记名称)标识某些提交时,例如其哈希ID以1234567...开头的提交,我们说该名称指向该特定提交。 The "true name" of the commit is the hash ID, and any number of (non-hash-ID) names —zero, one, two, a million—can all point to that commit. 提交的“真实名称”是哈希ID,并且任何数量的(非哈希ID)名称(零,一,二,一百万)都可以指向该提交。 The hash IDs are terrible for humans to use (who can remember a3c921fa60b7... for more than a few seconds?) so we generally prefer names. 哈希ID对于人类来说非常糟糕(谁还能记住a3c921fa60b7...几秒钟?),因此我们通常更喜欢使用名称。 When a commit is particularly important, we might give it a name, or several names. 当提交特别重要时,我们可以给它起一个名字或几个名字。

There is one very important way that a branch name is not like a tag name, though, and that is how Git behaves when you are "on" the branch, as in, when running git status says on branch xyz . 有一个分支的名字不像是一个标签名,但其中一个很重要的方式,那就是当你是“上”分支Git是如何表现,如,运行时, git statuson branch xyz In this case , Git will automatically change the branch name so that it points, not to the commit that it pointed to just a moment ago, but instead, to the new commit you just made. 在这种情况下 ,Git将自动更改分支名称,以使其指向而不是指向它刚才指向的提交,而是指向您刚刚进行的新提交。

Hence, if you're on branch xyz , and it points to commit 1234567 , and you make a new commit, the name xyz no longer points to 1234567 . 因此,如果您在分支xyz ,并且它指向提交1234567 ,并且进行了新的提交,则名称xyz不再指向1234567 Instead, it points to some new commit, maybe fedcba9 for instance. 相反,它指向一些新的提交,例如,例如fedcba9 The new commit you just made, meanwhile, points back to commit 1234567 . 同时,您刚刚进行的提交指向1234567提交。 This is how branches grow. 这就是分支增长的方式。

Something special that any name does, for any commit, is to make sure that both you and Git can find that commit easily. 任何名称对于任何提交所做的特殊操作都是确保您和Git都能轻松找到该提交。 Having found that commit, you can have Git find that commit's parent. 已经发现提交,你可以让Git发现提交的母公司。 Having found the parent, you can have Git find the grandparent. 找到父母之后,您可以让Git找到祖父母。 Having found that commit, you can have Git keep going through history. 找到提交后,您可以让Git继续浏览历史。

This is the history in the repository, and it's formed by these parent linkages between commits. 存储库中的历史记录,它是由提交之间的这些父级链接形成的。 Branch names point to the tip commit of any given branch, and starting at that commit, you—and Git—can work your way back in time to every previous commit. 分支名称指向任何给定分支的最尖端提交,并且从该提交开始,您和Git可以按时退回到每个先前的提交。 Every commit you visit, as you do this "walk back through time", is reachable from the branch tip, via this walk process. 可以通过此遍历过程从分支机构的顶端访问您每次访问时所做的“遍历时间”。

When you run git merge , you can 1 make Git create a new commit that has not one but two parents. 当您运行git merge ,您可以1使Git创建一个新提交,该提交没有一个父母,而是两个父母。 These commits are called merge commits , and when you (or Git) do the walking through history, you (or Git) will visit both parents, and their grandparents and so on. 这些提交被称为合并提交 ,当你(或GIT)通过历史来代步,你(或GIT)将访问双方父母和祖父母等。 All of these commits are reachable from the branch tip that got you to this merge commit. 所有这些提交都可以从将您带到此合并提交的分支提示中进行访问。

This reachability has the side effect of making sure that Git does not remove the commits. 这种可到达性具有确保Git不删除提交的副作用。 All reachable commits are permanent. 所有可到达的提交都是永久的。 All commits are read-only. 所有提交都是只读的。 So whatever is in history, reachable from all branch tips, is stored forever: read-only and permanent, in the form of commits. 因此,历史记录中所有可以从所有分支提示访问的内容都将被永久存储:只读和永久性的提交形式。

Once you merge branch feature into mainline , you can delete the name feature , because the name mainline either points to, or eventually reaches, the merge commit that points to the commits that were, earlier, only reachable from feature . 将分支feature合并到mainline ,您可以删除名称feature ,因为mainline名称指向或最终到达指向该feature的合并提交,该合并提交以前只能从feature到达。 So after merging, you can delete a name. 因此,合并后,您可以删除名称。 But there's no need to merge: you can keep the name, which keeps the commits. 但无需合并:您可以保留名称,保留提交。

Or, if you have determined that there's no need for these commits ever, in the future, you can delete the name. 或者,如果您确定不再需要这些提交,则将来可以删除名称。 This makes the commits unreachable —well, by that name at least; 这使得提交不可访问 -至少,至少这个名称是如此; there might still be some other name(s) that reach them. 可能还有其他一些名称可以使用。 If the commits are truly unreachable—if they have no branch name that finds them, no tag name that finds them, and no other name (such as refs/stash ) that eventually allows Git to find them from a name—then at some point soon, when git gc runs, Git will actually remove those commits for real. 如果提交确实是无法访问的(如果它们没有找到它们的分支名称,没有找到它们的标签名称,并且没有其他最终允许Git从名称中找到它们的名称(例如refs/stash )),则在某个时候很快,当git gc运行时,Git实际上会真正删除那些提交。

Hence, the answer is no: you don't have to merge a branch. 因此,答案是否定的:你不必将分支合并。 As long as you keep the name around, the name keeps all those commits preserved. 只要保留名称,该名称将保留所有这些提交。 If you have merged the branch, some other name is now also keeping those commits preserved, so you can (but do not have to) delete the branch-name; 如果已经合并了分支,那么现在还会使用其他名称来保留那些提交,因此您可以 (但不必)删除分支名称; but as long as you keep the branch name around, the commits stick around, whether merged or not. 但是只要您保持分支名称不变,提交就会一直存在,无论是否合并。 Those commits are reachable from the tip of the branch: something Git sometimes calls contained in the branch. 这些提交可以从分支的顶端到达:Git有时调用包含在分支的某些东西。 (See git branch --contains .) (请参见git branch --contains 。)


1 The git merge command does not always make merge commits. 1 git merge命令并不总是进行合并提交。 Sometimes, it does a "fast forward", which is not really a merge. 有时,它执行“快进”,这实际上不是合并。 You can tell it to --squash , which eventually makes an ordinary (non-merge) commit, too. 您可以将其告知--squash ,它最终--squash进行普通的(非合并)提交。 So git merge only makes merge commits sometimes . 所以git merge 有时只使合并提交。

不会。使用发行分支非常普遍:例如,对于每个主要发行版,您都可以在其发行分支上进行错误修复,以创建新的子发行版本,而同时在开发分支上并行完成主要开发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM