278

I have branch Master, branchA and branchB. Now I'm working in the branchA and I need to merge branchA with branchB and proceed my work in the branchA. All files are comitted in the branchA and branchB.

What's the fast way to implement it?

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
user3127896
  • 6,323
  • 15
  • 39
  • 65

8 Answers8

514

If I understood your question, you want to merge branchB into branchA. To do so,

first checkout branchA like below,

git checkout branchA

Then execute the below command to merge branchB into branchA:

git merge branchB
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • 8
    We need to make sure that branches A and B exist on local repository. Only then we can perform merge. – Santhosh Aug 29 '17 at 09:45
  • 16
    The question is explicit: `I have branch Master, branch A and branch B.` – Lee Goddard Jan 24 '19 at 23:26
  • I do this and got `Branch B|MERGING`. What does this mean? I want to have a final result of `Branch B` only. – Nemra Khalil Feb 04 '21 at 13:39
  • @NemraKhalil I can't tell what you're trying to do, it could be normal, could be something where Branch B doesn't matter (you start with branches a and b and you want to only be left with branch b, if that's what you mean then just delete branch a, tho I doubt this is what you want since you'd probably be on a very different question), merging is where two branches are combined into a single branch, so you might have added tabs on one branch and full screen support on another, you could merge them to have both on a single branch (as a massive oversimplification) – jgh fun-run Aug 11 '21 at 00:15
  • @Abimaran Kugathasan What is the command to merge say only 1 file from branch B into branch A ? Eg: There are 10 files in branch B modified but I want to merge only 1 file from branch B to branch A – Suresh May 12 '22 at 04:46
178

Here's a clear picture:

Assuming we have branch-A and branch-B

We want to merge branch-B into branch-A

on branch-B -> A: switch to branch-A

on branch-A: git merge branch-B
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83
  • 56
    this isn't clearer at all, just post the terminal input it's much easier to understand – lopu Jan 04 '18 at 09:43
  • 11
    This isn't clear enough answer .... what does it mean "update the branch-B" ... there are many ways of doing that, which one? ... instead of switch git checkout branch-A is more understandable – Erdinç Çorbacı Mar 22 '18 at 08:34
9

The answer from the Abiraman was absolutely correct. However, for newbies to git, they might forget to pull the repository. Whenever you want to do a merge from branchB into branchA. First checkout and take pull from branchB (Make sure that, your branch is updated with remote branch)

git checkout branchB
git pull

Now you local branchB is updated with remote branchB Now you can checkout to branchA

git checkout branchA

Now you are in branchA, then you can merge with branchB using following command

git merge branchB
2

on branchB do $git checkout branchA to switch to branch A

on branchA do $git merge branchB

That's all you need.

Dapo Momodu
  • 184
  • 1
  • 8
0

If you or another dev will not work on branchB further, I think it's better to keep commits in order to make reverts without headaches. So ;

git checkout branchA
git pull --rebase branchB

It's important that branchB shouldn't be used anymore.

For more ; https://www.derekgourlay.com/blog/git-when-to-merge-vs-when-to-rebase/

Erdinç Çorbacı
  • 1,187
  • 14
  • 17
  • 2
    In its title, the question relates to local repositories — for which `pull` will not work. – Lee Goddard Jan 24 '19 at 23:27
  • 2
    You are right, I just thought adding this knowledge would be a plus because probably local branch will be pushed at the end. But I missed the point you indicated, I would better add this as a comment not as an answer. Thanks for warning me. – Erdinç Çorbacı Jan 25 '19 at 11:35
0

For merging first branch to second one:

on first branch: git merge secondBranch

on second branch: Move to first branch-> git checkout firstBranch-> git merge secondBranch

-1

It is very simple, you need to first checkout to branchA, for this you can use command git checkout branchA. Now you are in branchA, just hit the merge command git merge branchB. And you are done!!!

S.R
  • 113
  • 11
-3

I would recommend below one if anyone looking to fetch remote changes as well:

git pull
git merge origin/development
Alok Gupta
  • 1,806
  • 22
  • 21