简体   繁体   English

用git合并多个分支

[英]Merging multiple branches with git

I have 2 local branches called "develop" and "master"; 我有2个本地分支机构,分别称为“开发”和“主管”; they are similar. 他们是相似的。 On my company's server there's one "main" repo (production) and several branches that were made by other developers: 在我公司的服务器上,有一个“主”仓库(生产)和其他开发人员建立的几个分支:

$ git branch -a
* develop
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/some-test
  remotes/origin/feature1
  remotes/origin/feature2
  remotes/origin/master

How can I merge remotes/origin/feature1 and remotes/origin/feature2 into my local "master" branch, copy that all into "develop" and start working with actual code in my "develop" branch? 如何将remotes/origin/feature1remotes/origin/feature2合并到我的本地“ master”分支中,全部复制到“ develop”中并开始在“ develop”分支中使用实际代码?

  1. git checkout master
  2. git pull origin feature1 feature2
  3. git checkout develop
  4. git pull . master git pull . master (or maybe git rebase ./master ) git pull . master (或者git rebase ./master

The first command changes your current branch to master . 第一条命令将您当前的分支更改为master

The second command pulls in changes from the remote feature1 and feature2 branches. 第二条命令从远程feature1feature2分支feature1更改。 This is an "octopus" merge because it merges more than 2 branches. 这是一个“章鱼”合并,因为它合并了两个以上的分支。 You could also do two normal merges if you prefer. 如果愿意,也可以进行两个普通合并。

The third command switches you back to your develop branch. 第三个命令将您切换回您的develop分支。

The fourth command pulls the changes from local master to develop . 第四个命令将更改从本地master拉到develop

Hope that helps. 希望能有所帮助。

EDIT: Note that git pull will automatically do a fetch so you don't need to do it manually. 编辑:请注意git pull将自动进行fetch因此您不需要手动进行。 It's pretty much equivalent to git fetch followed by git merge . 这几乎等同于git fetchgit merge

I would just "fetch" all of origin: 我只是“获取”所有来源:

git fetch origin

now that it is in your repo you can merge the branches into master: 现在它在您的仓库中,您可以将分支合并到master中:

git checkout master

git merge origin/feature1 

git merge origin/feature2 

now you can merge master into develop 现在您可以将master合并为development

git checkout develop
git merge master 

if you are going to commit back to origin then I would setup a local tracking branch so you can have local access and push directly to origin: 如果您要提交回原点,那么我将设置一个本地跟踪分支,以便您可以进行本地访问并直接推送到原点:

git branch --track origin/feature1 feature1

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

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