111

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?

1
  • Have you tried git-merge ? Help here.
    – karatedog
    Dec 20, 2010 at 15:21

1 Answer 1

143

You can either git merge master or git rebase master.

If the branch has not been distributed to other people, 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 nobody else is working on it, or there will be confusion and extra work for them, 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 ) feature@{1}
       /
      /       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 as feature@{1}.

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
8
  • 1
    You should explain why you prefer rebase and what the difference is. Rebase creates a linear history - this might not fit this question. Dec 20, 2010 at 18:47
  • 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
    Dec 20, 2010 at 22:28
  • 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"). Dec 21, 2010 at 6:40
  • 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. Dec 21, 2010 at 15:40
  • 8
    I have never seen a clearer explanation about the differences between merge and rebase. Thank you. Oct 8, 2018 at 20:59

Your Answer

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

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