Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between merging master into branch and merging branch into master?

Tags:

git

merge

I have a branch called master and another called dev. Usually, I do tests and improvements on dev; when done, I merge it into master, tag it, and release new version of the application.

Now, I face a decision to make in regard to merging:

  1. merge master into dev
  2. merge dev into master

but I am not really sure how the two are different... Any explanation would be welcome.

like image 271
sop Avatar asked Oct 15 '14 13:10

sop


People also ask

Does merging master into branch change master?

git merge master will update your current branch with the changes from your local master branch, the state of which will be that of when you last pulled while on that branch.

Is it OK to merge master into branch?

Every developer has a different Git branch management strategy, be it the popular GitFlow method or some other, home-grown concoction. But whatever branch management strategy you use, developers aren't supposed to merge master into branches. In fact, the exact opposite is supposed to happen.

Should you merge from master or branch?

Ideally, use smaller branches and commit/merge to main branch more frequently. Failing that, get to a clean commit point in your branch and merge/rebase latest master into your branch - keep it up to date.

What does it mean to merge to master?

If you merge dev into master If master is checked out ( git checkout master ), and you then merge dev ( git merge dev ), you will end up in the following situation: The master branch now points to the new merge commit ( F ), whereas dev still points to the same commit ( E ) as it did before the merge.


1 Answers

TL;DR

The main difference lies in where the master and dev branches end up pointing. enter image description here

Full explanation

Merging one branch into another is not a symmetric operation:

  • merging dev into master, and
  • merging master into dev,

are, in general, not equivalent. Here is an illustrative example that explains the difference between the two. Let's assume your repo looks as follows:

enter image description here

If you merge dev into master

If master is checked out (git checkout master),

enter image description here

and you then merge dev (git merge dev), you will end up in the following situation:

enter image description here

The master branch now points to the new merge commit (F), whereas dev still points to the same commit (E) as it did before the merge.

If you merge master into dev

If, on the other hand, dev is checked out (git checkout dev),

enter image description here

and you then merge master (git merge master), you will end up in the following situation:

enter image description here

The dev branch now points to the new merge commit (F', whereas master still points to the same commit as it did before the merge (D).

Putting it all together

enter image description here

like image 162
jub0bs Avatar answered Sep 17 '22 14:09

jub0bs