Git重用分支或删除并重新创建 [英] Git reuse branch or delete and create again
问题描述
我最近得到了一项任务,为我正在开发的一个项目添加一个特定的功能。由于这个特性依赖于那些残酷使用的旧代码,我决定分两步来分解任务:
我创建了一个分支feat / foo,重构完成后,我将它合并到我们的主人中,以便我们可以直接使用这些更改。现在我剩下以下提交历史记录:
A ---> B ---> C - > E ---> F< master B:创建分支专长/ foo
| ^ D:重构完成
D ----------- | C:在
^ E之间的主变化E:合并提交
feat / foo F:master现在在这里
feat / foo仍然指向D,并且我的主人进入提交F.我现在要做什么来继续我在分支feat / foo中执行此任务的工作?我看到两种可能性:
- 删除feat / foo和
checkout -b
它再次,所以我有一个新的分支,与我的旧分支名称相同 - 或以某种方式重复使用feat / foo,我不知道该怎么做 >
第一个解决方案对我来说感觉不太合适,删除分支只是为了再次创建它似乎是错误的。但我不知道如何重用它。
我该怎么办?删除并重新创建分支,或者如果正确的答案是重用它,怎么做?
在许多工作流程中,一旦某个功能分支已被合并回 master
它被删除。 GitHub可能就是这方面的最好例子。如果你遵循这个思想流派,你会删除 feat / foo
并为你的下一个冲刺创建一个新的功能分支。 如果你真的想继续使用这个分支,那么你必须在 master
上重新命名 feat / foo
>或将 master
合并到 feat / foo
中。我认为没有任何优势来重组,这可能很麻烦,所以我们考虑合并。您在提交 E
时将 feat / foo
合并为 master
。因此, master
已经具有 feat / foo
中的所有特性,但反过来并不正确,即 feat / foo
很可能缺少自$ commit D $ c $以来引入到
master
中的几项功能C>。要执行合并,您可以使用以下命令:
git checkout feat / foo
git merge master
您可能必须解决
$ b 现在,
feat / foo $ c $> foo
分支是最新的主人,你可以继续使用它,如果你愿意。就我个人而言,我只需将 feat / foo
放在原来的位置并创建一个全新的功能分支。您可以保留它几次冲刺,直到您确定删除它安全。 I recently got the task to add a certain feature to a project I'm working on. As this feature depends on old code that was cruel to use, I decided to split the task in 2 steps:
- refactor old code to be more usable
- create feature using refactored code
I created a branch feat/foo, and after the refactoring was done, I merged it into our master so we could use the changes directly. Now I'm left with the following commit history:
A ---> B ---> C --> E ---> F <master B: created branch feat/foo
| ^ D: refactoring finished
D -----------| C: changes in master in between
^ E: merge commit
feat/foo F: master is now here
feat/foo still points to D, and my master advanced to be at commit F. What would I do now to continue my work on this task in branch feat/foo? I see two possibilities:
- either delete feat/foo and
checkout -b
it again, so I have a new branch that has the same name as my old branch, - or somehow "reuse" feat/foo, which I don't know how to do
The first solution somehow feels not quite right to me, it seems "wrong" to delete the branch just to create it again. But I don't know how I could reuse it.
What should I do? Delete and recreate the branch, or if the right answer would be reusing it, how?
In many workflows, once a feature branch has been merged back into master
it is deleted. GitHub is probably the prime example of this. If you follow this school of thought, you would delete feat/foo
and create a new feature branch for your next sprint.
If you really want to keep using the branch, then you will either have to rebase feat/foo
on master
or merge master
into feat/foo
. I don't see any advantage to rebasing, which could be messy, so let's consider merging. You merged feat/foo
into master
at commit E
. Therefore master
already has all the features from feat/foo
, but the reverse is not true, i.e. feat/foo
is likely missing several features which have been introduced into master
since commit D
. To do the merge you would use this command:
git checkout feat/foo
git merge master
You may have to resolve merge conflicts arising from new features in master
which are not yet in the feat/foo
branch.
Now the feat/foo
branch is up to date with master, and you can keep using it if you wish. Personally, I would just leave feat/foo
where it is and create an entirely new feature branch. You can keep it around for a few sprints until you are sure that deleting it safe.
这篇关于Git重用分支或删除并重新创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!