Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to source and destination branches after git merge?

Tags:

git

When I run git checkout branch1 followed by git merge branch2 to merge two branches it seems like branch2 simply overwrites branch1. Should this be happening?

Edit:

Before merge

  • branch1 = readme.txt (Hello World!)
  • branch2 = readme.txt (Hello 2!)

After merge

  • branch1 = readme.txt (Hello 2!)

tbh, I'm not sure what a merge is technically supposed to do.

like image 880
LightBox Avatar asked Dec 23 '12 12:12

LightBox


People also ask

What happens to a branch of a branch after merge?

Let's go into merging in more detail. When you perform a merge, you effectively merge one branch into another—typically a feature branch or bug fix branch into a main branch such as master or develop. Not only will the code changes get merged in, but also all the commits that went into the feature branch.

Does git merge affect both branches?

No, merging does only affect one branch.

What happened after git merge?

After the merge, the working tree and the index and the HEAD (the new merge commit) are all synchronized. In other words, Git does not merely make a new commit; it also changes the state of your working tree (and the index).

What happens to a merging branch after a successful merge?

The answer is: nothing happens to the feature branch as a result of the merge. The new merge commit is added on the current branch (which is master when the merge is done).


2 Answers

When merging one branch into another git merge will apply all the commits from the branch being merged from to the branch being merged into since the two diverged. You can consider it as forming a new head that contains the latest state from both of the branches put together. If you have changed a file in a branch and you merge it back into it's parent then the changes will be applied on top of the current state of that branch. If both files have changed in the same place then merge may not be able to resolve this and you will have to intervene. Usually though you just end up with the latest work from both branches.

In your example the changes you made in branch2 have been merged into branch1. This appears to be changing the text to "Hello 2!".

The git-merge manpage has this to say about it:

Assume the following history exists and the current branch is "master":

          A---B---C topic
         /
    D---E---F---G master

Then "git merge topic" will replay the changes made on the topic branch since it diverged from master (i.e., E) until its current commit (C) on top of master, and record the result in a new commit along with the names of the two parent commits and a log message from the user describing the changes.

          A---B---C topic
         /         \
    D---E---F---G---H master

In short this is the correct operation of merge.

like image 130
Will Avatar answered Sep 18 '22 12:09

Will


If readme.txt diverged in branch1 and branch2, merging the branches will result in the file being in the state of conflict. The conflict is represented by both variants of diverging contents being present in the file delineated with conflict markers:

<<<<<<< HEAD:hello.txt
Hello world!
=======
Hello 2!
>>>>>>> 01234567890abcdef:hello.txt

A merge with conflicts will not be automatically committed. Instead, a message will be displayed instructing you to manually resolve the conflict and showing how to commit the changes:

CONFLICT (content): Merge conflict in <file>
Automatic merge failed; fix conflicts and then commit the result.

As pointed out by Nevik Rehnel, if the file didn't diverge in the branch, but was simply modified from a common shared contents (Hello world!) to a new content (Hello 2!), there will be no conflict and the merge operation will simply apply the changes. In other words, a merge does not guarantee merging of file contents, but merging of changes compared to a common base.

like image 31
user4815162342 Avatar answered Sep 20 '22 12:09

user4815162342