user689751
user689751

Reputation:

Git pull a branch from a different repository

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.

Upvotes: 26

Views: 38501

Answers (4)

vin
vin

Reputation: 671

You can also do

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

Upvotes: 15

pedrotester
pedrotester

Reputation: 189

You can just

git pull url branch

it works

Upvotes: 7

j2emanue
j2emanue

Reputation: 62549

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.

Upvotes: 6

Alireza
Alireza

Reputation: 4516

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

Upvotes: 32

Related Questions