Skip to content

Instantly share code, notes, and snippets.

@IanVaughan
Created June 7, 2012 09:57
Show Gist options
  • Save IanVaughan/2887949 to your computer and use it in GitHub Desktop.
Save IanVaughan/2887949 to your computer and use it in GitHub Desktop.
Push branch from one remote into another

Push branch from one remote into another

Add remote2 as a remote

$ git remote -v
remote2 git@github.com:repo2.git (fetch)
remote2 git@github.com:repo2.git (push)
origin  git@github.com:repo.git (fetch)
origin  git@github.com:repo.git (push)

Update local refs

$ git fetch remote2
remote: Counting objects: 13, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 7 (delta 4), reused 0 (delta 0)
Unpacking objects: 100% (7/7), done.
From git@github.com:repo2.git
 * [new branch]      1-0        -> remote2/1-0

Check branches

$ git br -ra 
* master
  remotes/remote2/1-0
  remotes/remote2/master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

Method 1

Involves checking out push and pushing from it

git co <remote1>/<branch>
git co -b <branch>
git push -u <remote2> <branch>

Do it

$ git co remote2/1-0
Note: checking out 'remote2/1-0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at b80ea8e... Deleted lockfile from repo.

Am detached so create/attach to same named branch

$ git co -b 1-0
Switched to a new branch '1-0'

Now push

$ git push -u origin 1-0
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:repo.git
 * [new branch]      1-0 -> 1-0
Branch 1-0 set up to track remote branch 1-0 from origin by rebasing.
@eduardrudko
Copy link

eduardrudko commented Jun 7, 2018

Thank you very much, just what I've been looking for.

@djomoutresor1
Copy link

sorry, but create concrete example, with real name

@seyfer
Copy link

seyfer commented Mar 21, 2021

git push remote2 'refs/remotes/remote1/*:refs/heads/*'
git push remote2 'refs/tags/*:refs/tags/*'

@aldrich2002
Copy link

thanks it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment