dotty
dotty

Reputation: 41503

Merging 2 branches together in Git

I've only just started to use Git and think it is 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'?

Upvotes: 272

Views: 588324

Answers (4)

knittl
knittl

Reputation: 265807

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

A little example:

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

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

$ # go back to branch A
$ git checkout A
$ # edit files
$ 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, check out 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---/

Alternatively, to create "regular" merge commits (with exactly two parents each), run git merge twice for each branch that you want to merge:

$ git merge B
$ git merge C

To get a history similar to:

…-o-o-x-------M-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

Upvotes: 334

Kishor Unnikrishnan
Kishor Unnikrishnan

Reputation: 2609

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 this merge commit and add as new 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 merge commit as our own.

Edit 2023: You can now have the option to change the merge commit interactively in newer versions of git without specifying the --no-commit option.

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

Upvotes: 5

Aditya Upadhyay
Aditya Upadhyay

Reputation: 55

To change your subBranch to main

git branch -m main <BRANCH>;

git fetch origin;

git branch -u origin/<BRANCH> <BRANCH>;

git remote set-head origin -a

Upvotes: -3

Mohamed Obaya
Mohamed Obaya

Reputation: 2583

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

Upvotes: 197

Related Questions