Skip to content

Instantly share code, notes, and snippets.

@aljgom
Forked from IanVaughan/README.md
Last active April 8, 2019 22:07
Show Gist options
  • Save aljgom/efb34c3df42b69d2c22aa8139ca13247 to your computer and use it in GitHub Desktop.
Save aljgom/efb34c3df42b69d2c22aa8139ca13247 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 add remote2 [email protected]:repo2.git

View remotes

$ git remote -v
remote2 [email protected]:repo2.git (fetch)
remote2 [email protected]:repo2.git (push)
origin  [email protected]:repo.git (fetch)
origin  [email protected]: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 [email protected]: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 [email protected]:repo.git
 * [new branch]      1-0 -> 1-0
Branch 1-0 set up to track remote branch 1-0 from origin by rebasing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment