Git 分支离开一个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34301568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:38:29  来源:igfitidea点击:

Git Branch Off A Branch

gitgithub

提问by sjmartin

I'm working off of master and create Branch 'A'.

我正在处理 master 并创建分支“A”。

Branch 'A' contains HTML/CSS/JS to create a 'widget'.

分支“A”包含用于创建“小部件”的 HTML/CSS/JS。

While this code is being reviewed I also want to work on creating tests for this 'widget'.

在这段代码的同时,我还想为这个“小部件”创建测试。

I can't work off of master yet because Branch 'A' hasn't been merged. But I need a way to work off of Branch 'A' without making updates to it while the code is being reviewed to push to master.

我还不能在 master 上工作,因为分支 'A' 还没有合并。但是我需要一种方法来处理分支“A”,而无需在代码以推送到主控时对其进行更新。

I figure I need to make Branch 'B' off of Branch 'A' so that I can continue working off of the code I had already created.

我想我需要将分支 'B' 与分支 'A' 分开,以便我可以继续使用我已经创建的代码。

Question 1. How can I do this in git?

问题 1. 我如何在 git 中做到这一点?

Question 2. Once I merge Branch 'A' to master, will that also include Branch 'B' even though i'm not done yet?

问题 2. 一旦我将分支 'A' 合并到 master,即使我还没有完成,它是否也包括分支 'B'?

Question 3. Can I merge Branch 'B' independent of Branch 'A'?

问题 3. 我可以独立于分支 'A' 合并分支 'B' 吗?

回答by Robbie Averill

Question 1. How can I do this in git?

问题 1. 我如何在 git 中做到这一点?

git checkout branch_a
git pull
git checkout -b branch_b

You'll then have the commits from branch_ain your new branch_b.

然后,您将branch_a在新的branch_b.

Question 2. Once I merge Branch 'A' to master, will that also include Branch 'B' even though i'm not done yet?

问题 2. 一旦我将分支 'A' 合并到 master,即使我还没有完成,它是否也包括分支 'B'?

No, they're entirely separate branches so only the commits from branch_awill exist in master.

不,它们是完全独立的分支,因此只有来自branch_amaster的提交才会存在。

Question 3. Can I merge Branch 'B' independent of Branch 'A'?

问题 3. 我可以独立于分支 'A' 合并分支 'B' 吗?

Sort of a two-pronged approach here. If you want to take branch_acommits with you, then you can merge branch_binto master at any time, although that sort of voids the purpose.

这里是一种两管齐下的方法。如果你想branch_a和你一起提交,那么你可以随时合并branch_b到 master 中,尽管这样会使目的无效。

Given that branch_b's tests presumably depend on branch_ait would seem silly to merge B before A.

鉴于那branch_b的测试大概取决于在branch_aA 之前合并 B 似乎很愚蠢。

Once you have merged A into master, you will probably need to rebase branch_bonto the new master, and get rid of all of the original commits you took over from branch_a, since they all now exist in master:

一旦您将 A 合并到 master 中,您可能需要 rebasebranch_b到新的 master 上,并摆脱您从 接管的所有原始提交branch_a,因为它们现在都存在于 master 中:

# Assume A merged into master
git fetch
# Rebase interactively
git rebase -i origin/master
# Exclude (skip or comment out) the commits that were from A, leaving only B
# Rebasing (n/n).....
git push origin branch_b --force

This will give you a fresh branch_bfrom master, where the commits from A now exist, and you won't have merge conflicts when you merge B into master.

这会给你一个新branch_b的 master,现在存在来自 A 的提交,当你将 B 合并到 master 时,你不会有合并冲突。

The force push will only be required if you've already pushed branch_bto origin. If you haven't, no problem.

仅当您已经推branch_b送到原点时才需要强制推送。如果你没有,没问题。

回答by Chris Tavares

It's trivial - you can create a branch off any branch in git.

这很简单 - 您可以在 git 中的任何分支上创建一个分支。

If you're on branch A, simply do git checkout -b Band you'll have a new branch starting at A. It will be separate from A, and changes from A will not be reflected in B.

如果您在分支 A 上,只需这样做git checkout -b B,您就会有一个从 A 开始的新分支。它将与 A 分开,并且 A 的更改不会反映在 B 中。

When A is merged to master, the merge will not bring in the commits on B.

当 A 合并到 master 时,合并不会带来 B 上的提交。

You can't manage B to master independently - branch B also contains the commits that were on A at the time it was created, so merging B will also bring in those commits as well. It is possible to bring in just the changes from B (without A) using an interactive rebase or a cherry-pick, but that's fairly advanced and probably not what you want to do.

您无法独立管理 B 以掌握 - 分支 B 还包含创建 A 时在 A 上的提交,因此合并 B 也会引入这些提交。可以使用交互式 rebase 或cherry-pick 只从 B(没有 A)引入更改,但这是相当先进的,可能不是您想要做的。

Another thing you'll want to do - once you branch off B, you'll probably end up making changes to A due to your code review. Be sure to rebase B on top of A to keep everything up to date, otherwise you'll have some pain later when you go to merge your tests into master.

您需要做的另一件事 - 一旦您从 B 分支出来,您可能最终会由于您的代码而对 A 进行更改。确保在 A 之上重新设置 B 以保持所有内容最新,否则稍后将测试合并到 master 时会遇到一些麻烦。