GIT merge master into a branch

277,195

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
Share:
277,195

Related videos on Youtube

Manish KUmar
Author by

Manish KUmar

Updated on September 17, 2022

Comments

  • Manish KUmar
    Manish KUmar over 1 year

    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?

    • karatedog
      karatedog over 13 years
      Have you tried git-merge ? Help here.
  • Andreas Rehm
    Andreas Rehm over 13 years
    You should explain why you prefer rebase and what the difference is. Rebase creates a linear history - this might not fit this question.
  • Manish KUmar
    Manish KUmar over 13 years
    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?
  • Christoffer Hammarström
    Christoffer Hammarström over 13 years
    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").
  • Manish KUmar
    Manish KUmar over 13 years
    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
  • Christoffer Hammarström
    Christoffer Hammarström over 13 years
    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.
  • Manish KUmar
    Manish KUmar over 13 years
    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 :)
  • khaled_webdev
    khaled_webdev over 6 years
    Is it from origin/master or from local/master?
  • Paulo Pedroso
    Paulo Pedroso over 5 years
    I have never seen a clearer explanation about the differences between merge and rebase. Thank you.