tags:

views:

48

answers:

1

Hay, I've only just started to use GIT and think its wonderful. However I'm a little confused over what the 'merge' does.

Say we have a working project in the brach called A

I got home and make changes to this brach and save it as B and another programmer makes changes to A and saves it as C

Is there any way to merge the two branches B and C together, then commit the changes as branch D?

Or am i missing the point of 'merge'?

+2  A: 

merge is used to bring two (or more) branches together.

a little example:

# on branch A:
# create new branch B
$ git checkout -b B
# hack hack
$ git commit -am "commit on branch B"

# create new branch C from A
$ git checkout -b C A
# hack hack
$ git commit -am "commit on branch C"

# go back to branch A
$ git checkout A
# hack hack
$ git commit -am "commit on branch A"

so now there are three separate branches (namely A B and C) with different heads

to get the changes from B and C back to A, checkout A (already done in this example) and then use the merge command:

# create an octopus merge
$ git merge B C

your history will then look something like this:

…-o-o-x-------A
      |\     /|
      | B---/ |
       \     /
        C---/

if you want to merge across repository/computer borders, have a look at git pull command, e.g. from the pc with branch A (this example will create two new commits):

# pull branch B
$ git pull ssh://host/… B
# pull branch C
$ git pull ssh://host/… C
knittl
I got a "content conflict" error when merging the branches.
dotty
that means that both branches did change the same piece of code. fix up the conflitcs, `git add conflicting_files` and then `git commit`
knittl
Say the file in A contained the word "hello", the A amended it to "HELLO" and B amended it to "Hello world". What would be the outcome of merging these files?
dotty
Whoops, i mean B amended it and C amended it.
dotty
be careful with the word 'amend' that means something different in git (changing existing commits). changing a file 'hello' to 'HELLO' on one side and 'Hello World' on the other side will result in a merge conflict
knittl
@dotty: While git often *seems* like magic, it can't actually read your mind - if two branches make two different changes to the same content, only a human can figure out how to reconcile them. That's what a merge conflict is.
Jefromi