Tao Huang
Tao Huang

Reputation: 1291

git push all branches from one remote to another remote

I have two remote: upstream and origin. upstream is something I can't push to. origin is my own repo. How can I fetch all branches from upstream and then push them to origin? I tried:

git fetch upstream
git push --all origin

But it doesn't work.

Upvotes: 93

Views: 55846

Answers (7)

ar13pit
ar13pit

Reputation: 76

If the upstream is already added as a remote on the repository, then the following works without explicitly checking out all branches:

git fetch -pPt upstream
git push origin --tags "refs/remotes/upstream/*:refs/heads/*"

If you would like to not push the tags, then remove the --tags option.

Upvotes: 1

jose.arias
jose.arias

Reputation: 1185

I just needed to copy one repository from Bitbucket to GitHub, these are the steps assuming your remote is called origin, all branches and tags will be copied:

git remote add neworigin url-to-new-remote
git push neworigin --tags "refs/remotes/origin/*:refs/heads/*"

Good thing about this is that files in your working copy won't be modified.

Upvotes: 115

Kyle Beard
Kyle Beard

Reputation: 664

You can run the following commands as a bash script with the first input being your existing source repo and the second input as your target repo.

mkdir migrate
git clone $1 migrate
cd migrate
git fetch origin
git remote add new-origin $2
git push -u new-origin --all
git push new-origin --tags
git remote set-head origin -d
git push new-origin refs/remotes/origin/*:refs/heads/*

This assumes you've already created the target repo.

Upvotes: 2

Lux.Capacitor
Lux.Capacitor

Reputation: 346

UI Method:
You can also do this via Github.com's UI - Private & Public Repos both work.

  1. Create a new repo in your organization on github.com - Click green "Create Repository" button Create new Repo Screen

  2. On next screen, the last option listed lets you clone your old repo to Github. (See Highlight in blue) Select Repository Source

  3. Next enter your old bitbucket repo url as below.
    (yes, private repo is fine - keep reading - next step is auth :p) Old repo url in bitbucket

  4. Lastly it will ask for your login credentials, enter them and go make some coffee. It wont take long honestly. Who might you be sir?

BAM - and you're done. Personally I just use the CLI + Mirror that is the accepted answer on here ironically, but a dev asked me about this on my team the other day and thought it'd be helpful to have the alternative.

Upvotes: 1

Nicko Glayre
Nicko Glayre

Reputation: 1405

You may want to try cloning your upstream repo with --mirror option and then push to your new remote with --mirror option too

You'll have the following flow:

git clone <upstream-repo-url/repo.git> --mirror
cd <repo>
git remote add <your-remote-name> <your-remote-url/repo.git>
git push <your-remote-name> --mirror

⚠ Be really careful with the push --mirror as it will delete branches that are on your <your-remote-name>

Upvotes: 92

Benyamin Jafari
Benyamin Jafari

Reputation: 34236

When you git push <REMOTE> --all or git push <REMOTE> --tags all branches and tags will push from your local history into the REMOTE. In this way, if you want push all of the branches and tags from a remote (i.e. origin) (not only your local history) to another remote (i.e. upstream) do the following procedure:

  1. Recieve all branches and tags from the origin and remove unmatched branches in your local history with the origin remote:
    • git fetch --prune
      
    • git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
      
    • git fetch --all
      
  2. Add new remote url:
    • git remote add upstream <the-url-path-of-a-remote.git>
      
  3. Now, your local is synchronized. Next, you can push all of the branches and tags to the new remote:
    • git push --all upstream
      git push --tags upstream
      

TL;DR

git fetch --prune
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git remote add upstream <the-url-path-of-a-remote.git>
git push --all upstream
git push --tags upstream

Upvotes: 10

RobG
RobG

Reputation: 822

One complete answer of cloning from one (bare) repository to another (bare) repository taking ALL branches, not just the checked out ones, is to clone a local bare repository as an intermediary. Then all branches are pulled as part of the clone and a git push --all will push them all. Example performed on Windows from github to gitlab:

  1. git clone --bare [email protected]:robe070/cookbooks.git
  2. cd cookbooks.git
  3. git remote add gitlab [email protected]:robe071/cookbookstest2.git
  4. git push --force --all gitlab
  5. git push --force --tags gitlab

Result: 25 branches pushed to gitlab

Note, git checkout is not required for all the branches and meaningless to a bare repo anyway.

Upvotes: 16

Related Questions