26

I've few files in my current repository. I want to merge a remote branch from a different repository.

  1. Pull and merge a branch from github.com/username/code.git (branch loader)
  2. Then pull and merge a branch from github.com/username/code.git (branch login)

Is it possible or what's the workaround? I wanna keep adding code to my current branch from different remote branches.

2
  • 5
    Can anyone correct me if I'm wrong instead negative voting? Commented Sep 16, 2016 at 19:26
  • Yes, it's possible. A repository can have more than one remote defined, and you can push, fetch, pull, etc. to/from any of them. git help remote should point you in the right direction for adding additional remote repositories... Commented Sep 16, 2016 at 19:49

4 Answers 4

32

You can add other origins for your repository using

git remote add new_origin git@theUrlToRepo 

You can now start pushing/pulling and basically doing all operations on both of your remotes.

git push origin master git push new_origin master git pull origin master git pull new_origin master 

You just have to specify which remote you're doing your operations with and you're set.

In your specific usecase, after adding remote repos and naming them, you'd do something like the following to merge remote branches into your local workspace.

git merge origin/loader git merge new_origin/login 
Sign up to request clarification or add additional context in comments.

2 Comments

with git merge origin/loader, you are merging remote loader with local master/current branch, right?
Yes,thats what it does
15

You can also do

git pull <git_pull_url> <branch> --allow-unrelated-histories 

1 Comment

brother your answer just saved my life thank you was searching for this answer and found it here
7

You can just

git pull url branch

it works

Comments

6

do this from your original repo that you want to merge the new code into:

for me i just created a new branch with the same name first:

git checkout -b my_new_branch 

then i added the other origin:

git remote add new_origin http://someRepo.git 

then i simply pulled the branch from new_origin into my current repo:

git pull new_origin my_new_branch 

the git pull command should do a fetch and merge for you.

Comments