Merging 2 branches together in GIT

369,289

Solution 1

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

Solution 2

If you want to merge changes in SubBranch to MainBranch

  1. you should be on MainBranch git checkout MainBranch
  2. then run merge command git merge SubBranch

Solution 3

Case: If you need to ignore the merge commit created by default, follow these steps.

Say, a new feature branch is checked out from master having 2 commits already,

  • "Added A" , "Added B"

Checkout a new feature_branch

  • "Added C" , "Added D"

Feature branch then adds two commits-->

  • "Added E", "Added F"

enter image description here

Now if you want to merge feature_branch changes to master, Do git merge feature_branch sitting on the master.

This will add all commits into master branch (4 in master + 2 in feature_branch = total 6) + an extra merge commit something like 'Merge branch 'feature_branch'' as the master is diverged.

If you really need to ignore these commits (those made in FB) and add the whole changes made in feature_branch as a single commit like 'Integrated feature branch changes into master', Run git merge feature_merge --no-commit.

With --no-commit, it perform the merge and stop just before creating a merge commit, We will have all the added changes in feature branch now in master and get a chance to create a new commit as our own.

Read here for more : https://git-scm.com/docs/git-merge

Share:
369,289

Related videos on Youtube

dotty
Author by

dotty

Hmm not alot really.

Updated on May 17, 2020

Comments

  • dotty
    dotty almost 4 years

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

    Let us say we have a working project in the branch "A".

    I go home and make changes to this branch and save it as "B". Another programmer makes changes to "A" and saves it as "C".

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

    Or am missing the point of 'merge'?

    • knittl
      knittl almost 14 years
      no need to 'save changes' in another branch. work an A and then merge those different A's together
    • dotty
      dotty almost 14 years
      I don't follow. A would be on my local computer, dev1 would have a copy of A on his computer, and dev2 has a copy of A on his computer. Both devs make changes, how do i merge these changes together?
    • knittl
      knittl almost 14 years
      see <kernel.org/pub/software/scm/git/docs/git-pull.html>. the devs probably need to push/upload their repository somewhere first
  • knittl
    knittl almost 14 years
    that means that both branches did change the same piece of code. fix up the conflitcs, git add conflicting_files and then git commit
  • dotty
    dotty almost 14 years
    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
    dotty almost 14 years
    Whoops, i mean B amended it and C amended it.
  • knittl
    knittl almost 14 years
    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
  • Cascabel
    Cascabel almost 14 years
    @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.
  • Stryker
    Stryker about 8 years
    This worked perfectly. Some issues I ran into which I have posted as well. but other than that it was perfec.
  • Marcelo Scofano Diniz
    Marcelo Scofano Diniz about 4 years
    @luckytaxi: Agreed, sometimes less is more
  • Yohanelly
    Yohanelly almost 4 years
    Can I undo this ?
  • Mohamed Shaban
    Mohamed Shaban over 3 years
    @Yohanelly yes, you can reset git head to any commit.
  • Ice Bear
    Ice Bear over 3 years
    (one the MainBranch) so what this would mean is git merge SubBranch Where SubBranch would be the branch would be the basis of the merge. So any changes to SubBranch would be applied to MainBranch right? Thanks! I tried it out