GIT merge master into a branch
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
Related videos on Youtube
Manish KUmar
Updated on September 17, 2022Comments
-
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 over 13 yearsHave you tried git-merge ? Help here.
-
-
Andreas Rehm over 13 yearsYou should explain why you prefer rebase and what the difference is. Rebase creates a linear history - this might not fit this question.
-
Manish KUmar over 13 yearsOk 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 over 13 yearsNo, 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 withgit checkout -b test-feature feature
(assuming your feature branch is named "feature"). -
Manish KUmar over 13 yearsI 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 over 13 yearsWhat 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 usegit merge
instead. And you can usegit reflog
to find your previous feature branch head if you want to get it back. -
Manish KUmar over 13 yearsahh 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 over 6 yearsIs it from origin/master or from local/master?
-
Paulo Pedroso over 5 yearsI have never seen a clearer explanation about the differences between merge and rebase. Thank you.