35

I have 2 local branches called "develop" and "master"; 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?

2 Answers 2

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

The first command changes your current branch to master.

The second command pulls in changes from the remote feature1 and feature2 branches. 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.

The fourth command pulls the changes from local master to develop.

Hope that helps.

EDIT: Note that git pull will automatically do a fetch so you don't need to do it manually. It's pretty much equivalent to git fetch followed by git merge.

4
  • 2
    What's the resulting difference between this method and just using two merges? Do you you get a single merge commit with all three branches as parents? Also, this will presumably fail if the merges aren't clean, right? So a git merge feature1 feature2 might be necessary anyway..
    – naught101
    Mar 13, 2013 at 5:35
  • To answer my own question, you do get a single commit with the head of each branch as a parent (including the branch being merged into). if one or more of the individual merges would be fast forward merges, then you don't get the head of the branch being merged into as a parent, because it's already in the history of one of the branches.
    – naught101
    Mar 13, 2013 at 5:52
  • 20
    If there is a conflict, the merge will simply fail with the message Should not be doing an Octopus.. This is clearly one of the best error messages ever.
    – naught101
    Mar 13, 2013 at 5:58
  • Can the fourth command be substituted with git merge master? I want to make sure I understand the difference between git pull and git merge.
    – Web User
    Jul 6, 2016 at 18:16
14

I would just "fetch" all of origin:

git fetch origin

now that it is in your repo you can merge the branches into master:

git checkout master

git merge origin/feature1 

git merge origin/feature2 

now you can merge master into develop

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.