git 如何将分支合并到 master 但继续在该分支上工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26024586/
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 10:25:54  来源:igfitidea点击:

How can I merge a branch into master but continue working on the branch?

gitgithub

提问by jgravois

I created a branch to try a different approach and it worked, so I would like to merge the "Farmcrops" branch into "Master" to keep those changes but continue the "Farmcrops" branch to explore another possibility. That way, if this latest change doesn't work, I can revert back to "Master" which would now include the first round of changes.

我创建了一个分支来尝试不同的方法并且它起作用了,所以我想将“Farmcrops”分支合并到“Master”以保留这些更改,但继续“Farmcrops”分支以探索另一种可能性。这样,如果最新的更改不起作用,我可以恢复到“主”,现在将包括第一轮更改。

How can I do that?

我怎样才能做到这一点?

采纳答案by Jo?l Salamin

Here is the process you are looking for:

这是您正在寻找的过程:

  1. git checkout master
  2. git merge Farmcrops
  3. git push origin master
  4. git branch -d Farmcrops
  5. git checkout master
  6. git checkout -b Farmcrops
  7. continue your commits on the branch Farmcrops...
  1. git checkout master
  2. git merge Farmcrops
  3. git push origin master
  4. git branch -d Farmcrops
  5. git checkout master
  6. git checkout -b Farmcrops
  7. 继续你在分支上的提交Farmcrops......

Branches are just pointers, it's very easy to create/delete a branch and if your branch Farmcropsisn't pushed on remote repository, there is absolutely no dependency with it. You can delete it after the merge and recreate it from the master.

分支只是指针,创建/删除分支非常容易,如果您的分支Farmcrops没有推送到远程存储库,则绝对没有依赖关系。您可以在合并后将其删除并从母版重新创建它。

Hope this will help you.

希望这会帮助你。

回答by jub0bs

If I understand correctly, you're starting from

如果我理解正确,你是从

-- o -- o -- o [master]
    \
     o -- o [Farmcrops]

You shouldn't merge Farmcropsdirectly into master, because you run the risk of breaking the code in master, which, by convention, is supposed to be more stable. Instead, check out Farmcropsand merge masterinto it.

您不应该Farmcrops直接合并到 中master,因为您冒着破坏 中代码的风险master,按照惯例,这应该更稳定。相反,检查Farmcrops并合并master到它。

git checkout Farmcrops
git merge master

Then you'll get

然后你会得到

-- o -- o -- o [master]
    \         \
     o -- o -- o [HEAD -> Farmcrops]

Run some tests; make sure everything works as expected. Then check out masterand merge Farmcropsinto it:

运行一些测试;确保一切都按预期工作。然后检出master并合并Farmcrops进去:

git checkout master
git merge Farmcrops

Your repo will then look like this:

您的回购将如下所示:

-- o -- o -- o
    \         \
     o -- o -- o [HEAD -> master,Farmcrops]

Now check out Farmcropsagain and continue your experiment, make more commits on it, etc...

现在Farmcrops再次检查并继续您的实验,对其进行更多提交,等等......

-- o -- o -- o
    \         \
     o -- o -- o [master]
                \
                 o -- o -- o [HEAD -> Farmcrops]

You can always fall back on master(which now contains "the first round of changes", as you put it) if your new experiment on Farmcropsdoesn't pan out so well.

master如果你的新实验Farmcrops没有那么顺利,你总是可以依靠(现在包含“第一轮更改”,正如你所说的那样)。

回答by wasabi

In the link below it is explained how to create a hotfix branch, make changes and merge it to the master. Only difference, hotfix branch is deleted after merge.

在下面的链接中,解释了如何创建修补程序分支、进行更改并将其合并到主分支。唯一不同的是,合并后会删除修补程序分支。

Just use Farmcrops as a branch name and do not delete branch after merge.

只需使用 Farmcrops 作为分支名称,合并后不要删除分支。

GIT-SCM: Basic Branching and Merging

GIT-SCM:基本分支和合并

[STEP 1] Create a branch and make your changes

[STEP 1] 创建一个分支并进行更改

$ git checkout Farmcrops
Switched to a new branch 'Farmcrops'
$ vim index.html
$ git commit -a -m 'fix the broken email address'
[Farmcrops 3a0874c] fix the broken email address
 1 files changed, 1 deletion(-)

[STEP 2] Then, go back to master branch and merge

[STEP 2] 然后,回到 master 分支并合并

$ git checkout master
$ git merge Farmcrops
Updating f42c576..3a0874c
Fast-forward
 README | 1 -
 1 file changed, 1 deletion(-)

And, if you want to make more changes on the same branch, apply [step 1] again.

而且,如果您想在同一分支上进行更多更改,请再次应用 [步骤 1]。

When you complete changes apply [step 2] again.

完成更改后,再次应用 [步骤 2]。

Follow these steps as much as required.

根据需要尽可能多地执行这些步骤。

Once you finished your job with this branch you can delete it.

完成此分支的工作后,您可以将其删除。

$ git branch -d Farmcrops
Deleted branch Farmcrops (was 3a0874c).

NOT: Instead of merge I suggest rebase GIT-SCM: Rebase

不是:我建议使用 rebase GIT-SCM代替合并:Rebase

git rebase Farmcrops

回答by albingroen

  1. Merge feature branch PR
  2. Delete feature branch on GitHub
  3. git branch -d feature-branch
  4. git checkout -b feature-branch
  1. 合并功能分支 PR
  2. 删除 GitHub 上的功能分支
  3. git branch -d feature-branch
  4. git checkout -b feature-branch