[英]why should I delete feature branches when merged to master
Most of the git workflows I've seen suggest to delete a branch
after it's been merged into master. 我见过的大多数git工作流程都建议将branch
合并到master后删除该branch
。 For example, this gitflow suggests the following: 例如,此gitflow建议以下内容:
# Incorporating a finished feature on develop
$ git checkout develop
Switched to branch 'develop'
$ git merge --no-ff myfeature
Updating ea1b82a..05e9557
(Summary of changes)
$ git branch -d myfeature
Deleted branch myfeature (was 05e9557).
$ git push origin develop
Why should I delete the branch? 为什么要删除分支? I'm also curious what to do when later a bug is discovered that was introduced by the feature - should I create the branch with the same name again, fix bug there, merge into master and again delete the branch? 我也很好奇,以后发现该功能引入的错误时该怎么办-我是否应该再次创建具有相同名称的分支,在那里修复错误,合并到master并再次删除该分支?
Something important to realize is Git branches are nothing more than a label pointing to a commit. 重要的是要认识到,Git分支不过是指向提交的标签。 Branching in Git is literally branching. 在Git中分支实际上是分支。 Here's what a repository looks like if feature
branched off master
when master
was a commit B. 这是当master
是提交B时,如果feature
从master
分支出来的话,存储库的外观。
A - B - C - F - H [master]
\
D - E - G - I[feature]
See? 看到? Actual branch. 实际分支。 When you git merge feature
into master, you get this. 当您git merge feature
到master中时,您会得到这个。
A - B - C - F - H - J [master]
\ /
D - E - G - I [feature]
And once you git branch -d feature
the branch history remains! 并且一旦您使用git branch -d feature
,分支历史便会保留!
A - B - C - F - H - J [master]
\ /
D - E - G - I
J has the parents H and I. J cannot exist without them, it's baked into how Git works. J有H和I的父母。没有他们,J就不会存在,它融入了Git的工作原理。 I cannot exist without G. G cannot exist without E. And so on. 没有G,我就不会存在。没有E,G就不会存在。依此类推。 The branch must remain 分支必须保留
J is a merge commit which will typically contain the name of the branch being merged. J是一个合并提交,通常将包含要合并的分支的名称。 Its like any other commit, so you can even add more information to it, like a link back to your issue tracker. 就像任何其他提交一样,因此您甚至可以向其添加更多信息,例如返回到问题跟踪器的链接。
git merge --no-ff
is used to prevent Git from doing a "fast forward" and losing the branch history. git merge --no-ff
用于防止Git进行“快进”并丢失分支历史记录。 This happens if no work has been done on master
since the branch was created. 如果自创建分支以来未在master
上进行任何工作,则会发生这种情况。 A fast-forward looks like this. 快进看起来像这样。
A - B[master]- D - E - G - I [feature]
git checkout master
git merge feature
A - B - D - E - G - I [feature] [master]
Since master
is a direct ancestor of feature
, no merge is required. 由于master
是feature
的直接祖先,因此不需要合并。 Git can just move the master
label. Git只能移动master
标签。 Your branch history is lost, it looks like D, E, G and I were all done as individual commits on master. 您的分支历史记录丢失了,看起来像D,E,G和我一样都是在master上单独提交。 git merge --no-ff
tells Git to never do this, to always perform a merge. git merge --no-ff
告诉Git永远不要执行此操作,以始终执行合并。
In the future, when it's noticed a bug was introduced in G, anyone browsing the repository can see it was done as part of the branch, look ahead to find the merge commit, and get information about the branch from there. 将来,当发现G中引入了一个错误时,任何浏览存储库的人都可以看到它已作为分支的一部分完成,并向前寻找合并提交,并从那里获取有关分支的信息。
Even so, why delete the branch? 即使这样,为什么还要删除分支? Two reasons. 有两个原因。 First, it will clutter up your list of branches with dead branches. 首先,它将使您的带有死枝的分支列表变得混乱。
Second, and more important, it prevents you from reusing the branch. 其次,更重要的是,它阻止您重用分支。 Branching and merging are complicated. 分支和合并很复杂。 Single use, short lived feature branches simplify the process by ensuring you only ever merge the branch back into master once . 一次性使用的短暂功能分支可确保您只将分支合并回master 一次,从而简化了流程。 This eliminates many technical and management problems. 这消除了许多技术和管理问题。 When you merge a branch, it is done . 合并分支后,它就完成了 。 If you need to fix a problem introduced in that branch, just treat it as a bug in master and make a new branch to fix it. 如果您需要解决该分支中引入的问题,只需将其视为master中的错误,然后创建一个新的分支来修复它。
Unfortunately, git log
lies to the user and presents a linear representation of history that is not linear. 不幸的是, git log
对用户而言是谎言,并且呈现的历史记录不是线性的线性表示。 To fix this, use git log --graph --decorate
. 要解决此问题,请使用git log --graph --decorate
。 This will draw lines as in my examples above, and show you any branches and tags on each commit. 如上面的示例所示,这将画线,并向您显示每次提交的所有分支和标签。 You'll get a much truer view of the repository. 您将获得更真实的存储库视图。
If you're on a Mac, GitX will visualize the repository for you. 如果您使用的是Mac, GitX将为您可视化存储库。 gitk is the generic version. gitk是通用版本。
Because the myfeature
branch history represents all the intermediate commits done to implement myfeature
. 因为myfeature
分支历史记录表示实现myfeature
所做的所有中间提交。
This strategy keeps only one commit in master
, and forget about the intermediate steps, which makes sense for long-lived branches, as I explained in " Why does git fast-forward merges by default? ". 正如我在“ 为什么默认情况下git快速转发合并? ”中所解释的,此策略仅在master
保留一个提交,而忽略了中间步骤,这对于存在很长的分支来说是有意义的。
The commit message of the one commit done (merged) in master
should make it clear it is done for implementing ' myfeature
'. 在master
完成(合并)的一次提交的提交消息应明确说明它已完成以实现“ myfeature
”。
If it needs to be fixed, the branch name can be reused (since it was deleted before). 如果需要固定,可以重用分支名称(因为以前已将其删除)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.