GIT merge master into a branch

53

16

I have been developing a new feature on a new branch, and on the side have committed quite a few changes on my master branch.

Is it possible to merge the master branch into my new branch to keep it up-to-date so that I won't have too many merge conflicts once the new feature is finished?

mnml

Posted 2010-12-20T15:03:46.203

Reputation: 1 391

Have you tried git-merge ? Help here.

– karatedog – 2010-12-20T15:21:40.200

Answers

62

You can either git merge master or git rebase master, in this case i would prefer git rebase.

Because git rebase makes it as if the changes on the feature branch were made on top of the changes on the master branch, which makes the version graph simpler.

Rebase

Taking the example from the git rebase manual, git rebase master in branch feature:

      A---B---C feature                             A'--B'--C' feature
     /                   --rebase-->               /
D---E---F---G master                  D---E---F---G master

However, git rebase is only suitable when the branch hasn't been distributed, or there will be confusion and extra work downstream, because the old commits A, B, C are now replaced by new commits A', B', C', plus F and G that were not there before.

The actual result after git rebase master in branch feature is this:

      ( A---B---C )
       /
      /       A'--B'--C' feature
     /       /
D---E---F---G master

Commits A, B, C are dangling after the rebase, but are reachable through git reflog feature.

Merge

If someone has pulled your branch, or you have pushed it somewhere, you should merge into it instead, to avoid confusion and extra work on the other end. See Recovering from upstream rebase.

This is the result of git merge master in branch feature:

      A---B---C feature                    A---B---C---M feature
     /                   --merge-->       /       ,---’
D---E---F---G master                 D---E---F---G master

Alternatively, if you git merge feature in branch master, it would look like this:

      A---B---C feature                    A---B---C feature
     /                   --merge-->       /         \
D---E---F---G master                 D---E---F---G---M master

Christoffer Hammarström

Posted 2010-12-20T15:03:46.203

Reputation: 764

Is it from origin/master or from local/master? – khaled_webdev – 2017-11-29T08:26:19.993

1I have never seen a clearer explanation about the differences between merge and rebase. Thank you. – Paulo Pedroso – 2018-10-08T20:59:23.040

You should explain why you prefer rebase and what the difference is. Rebase creates a linear history - this might not fit this question. – Andreas Rehm – 2010-12-20T18:47:30.813

Ok if I understand it well: I have to checkout the master branch if the newfeature is not finished yet and a rebase if it's finished? – mnml – 2010-12-20T22:28:44.413

No, with the feature branch checked out, do git rebase master, and it will "rebase" the changes in the feature branch so that they are "based" on the changes in the master branch. If the changes in the master branch conflict with the changes in the feature branch, git will ask you to resolve them and continue, skip them, or abort. If you feel unsure you can checkout a test branch to try it on with git checkout -b test-feature feature (assuming your feature branch is named "feature"). – Christoffer Hammarström – 2010-12-21T06:40:52.353

I have done my rebase but now when I pull from another computer I can't see my branch anymore: I have done git checkout newfeature / git rebase master – mnml – 2010-12-21T14:32:40.310

2

What do you mean "can't see my branch anymore"? Anyway, git rebase should only be used if the branch hasn't been distributed, which i assumed was the case since you said it was a new branch, sorry about that. See Recovering from upstream rebase in the docs i linked to. You'll have to use git merge instead. And you can use git reflog to find your previous feature branch head if you want to get it back.

– Christoffer Hammarström – 2010-12-21T15:40:34.083

ahh ok no worries I have a copy in another folder I'm just trying to understand how to use those things. It's a bit weird tho because now I have an undistributed branch called newfeature and I don't know how to push it :) – mnml – 2010-12-21T16:51:07.943