Skip to content

Instantly share code, notes, and snippets.

@hlucasfranca
Forked from IanVaughan/README.md
Created May 9, 2022 15:43
Show Gist options
  • Save hlucasfranca/b19773b694bf15604fa88d24730ddfa6 to your computer and use it in GitHub Desktop.
Save hlucasfranca/b19773b694bf15604fa88d24730ddfa6 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment