300

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?

9 Answers 9

563

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 

You can also condense this into one liner:

git merge branchA branchB 
Sign up to request clarification or add additional context in comments.

6 Comments

We need to make sure that branches A and B exist on local repository. Only then we can perform merge.
The question is explicit: I have branch Master, branch A and branch B.
I do this and got Branch B|MERGING. What does this mean? I want to have a final result of Branch B only.
@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)
@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
|
176

Here's a clear picture:

Assuming we have branch A and branch B and we want to merge branch B into branch A:

  1. On branch B: switch to branch A
  2. On branch A: git merge branch B

2 Comments

this isn't clearer at all, just post the terminal input it's much easier to understand
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
10

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 

1 Comment

Why would you pull during this process? The question says local.
2

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

on branchA do $git merge branchB

That's all you need.

Comments

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

Comments

0

Well, well...

Assuming we have branch AbadDeleted and branch Bgood and we want to merge branch AbadDeleted to into branch Bgood. Some may say why bad branch to good branch. Well, I made changes locally that was merged remotely and deleted remotely, so it is "bad" local branch. I have new remote Bgood branch, so I pulled it to local. Now I want to merge my changes from AbadDeleted to new Bgood branch.

  1. On branch AbadDeleted: switch to branch Bgood

  2. On branch Bgood: git merge branch AbadDeleted.

  3. Now I have to go to the each changed file (they are RED) and resolve manually conflicts :( :( :(

    I have so many files changed about 50, all of them have the manual conflicts that is easier for me to rename local folder, pull fresh new clone from remote, then copy all the files from renamed folder to the new folder and push back. Nice clean and understandable from Windows perspective.

Comments

-1

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/

2 Comments

In its title, the question relates to local repositories — for which pull will not work.
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.
-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!!!

1 Comment

ye, but about same was written here already says 2014 and yours 2011.
-3

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

git pull git merge origin/development 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.