Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge a remote branch into another local branch

Tags:

git

github

This might be a duplicate question, but I couldn't figure out how can I go about it. I'm trying to merge a remote branch say remoteBranch which is not a master branch to my local branch localBranch.

One of my developer added a new branch for an api end point on remote branch remoteBranch. As a frontend developer, I need to fetch that branch and merge it with my local development branch localBranch to make use of that api end point. How can I do this?

like image 377
user1012181 Avatar asked Aug 17 '17 13:08

user1012181


4 Answers

Simply merge it.

git fetch
git checkout localBranch
git merge remoteBranch
like image 196
hspandher Avatar answered Oct 26 '22 22:10

hspandher


According to the documentation of git-merge you can merge any other branch with your local branch.

Your current branch has to be your localBranch. To merge the remote branch simply type:

git merge remoteName/remoteBranch

In this case I assumed the name of your remote that contains the branch you need to be called remoteName. It may be called differently like origin or upstream. You need to make sure that your local reference to the remove branch is up to date. So perform a fetch command before doing the merge like so:

git fetch remoteName

Does this help you?

like image 39
Nitram Avatar answered Oct 26 '22 21:10

Nitram


To merge remoteBranch into localBranch

git fetch
git merge localBranch remoteName/remoteBranch

where remoteName is probably "origin", you can find that by git remote -v

However, sometimes you may want to rebase (re-writing history to keep sequence of commits "clean") instead of merge (which also adds a merge commit)

Merge vs. Rebase

You can rebase a remoteBranch into a localBranch using:

git fetch
git checkout localBranch
git rebase remoteName/remoteBranch

ref: https://www.atlassian.com/git/tutorials/merging-vs-rebasing

like image 20
Sam Avatar answered Oct 26 '22 22:10

Sam


Can use the following too, to achieve same:

git pull origin origin-branch-name:local-branch-name

above will merge the origin branch with local branch, keeping active intact

git pull another-local-branch:another2-local-branch

it should (not tested) merge the two different branches while keeping active intact

like image 43
justnajm Avatar answered Oct 26 '22 22:10

justnajm