git push all branches from one remote to another remote
Solution 1
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>
Solution 2
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.
Solution 3
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:
- git clone --bare [email protected]:robe070/cookbooks.git
- cd cookbooks.git
- git remote add gitlab [email protected]:robe071/cookbookstest2.git
- git push --force --all gitlab
- 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.
Solution 4
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:
- 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
-
- Add new remote url:
-
git remote add upstream <the-url-path-of-a-remote.git>
-
- 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
Solution 5
UI Method:
You can also do this via Github.com's UI - Private & Public Repos both work.
-
Create a new repo in your organization on github.com - Click green "Create Repository" button
-
On next screen, the last option listed lets you clone your old repo to Github. (See Highlight in blue)
-
Next enter your old bitbucket repo url as below.
(yes, private repo is fine - keep reading - next step is auth :p) -
Lastly it will ask for your login credentials, enter them and go make some coffee. It wont take long honestly.
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.
Tao Huang
A university student of Software Engineering from China~
Updated on June 22, 2021Comments
-
Tao Huang over 1 year
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.
-
RobG almost 4 yearsThis will only push branches that have been checked out locally. It does not pull ANY branches anyway
-
RobG almost 4 yearsOur need was to take an internally hosted git repo and move it to GitLab. Because our repo was not visible outside the firewall we could not use GitLab's importing or pull mirroring options (on Gitlab.com). We also had an option to use the GitLab Community Edition for which we could use importing, but with a 3 hour limit that our largest repos would exceed. And pull mirroring is not available in GitLab CE. So the option above will always work because it only relies on git functionality.
-
Pavel Rogovoy over 3 yearsBe really careful with the push --mirror as it will delete branches that on your <your-remote-name>
-
David Tchepak almost 3 yearsIs
--force
required? Maybe add--tags
to include these? -
jose.arias over 2 years@DavidTchepak Oh yes, you're right. --force isn't needed, In my case was because I had conflicting code in the target repo. And yes, --tags will copy the tags. I have updated my answer. Thanks.
-
Nejuf over 2 yearsI like this answer for repos that are already checked out. The only downside I see is that it pushes "HEAD" as a branch to the remote in addition to the local branches and remote branches.
-
David Ammouial over 2 yearsHow would you do if you don't want to create a clone, just to work from your current repo?
-
Toby 1 Kenobi over 2 yearsthe second line gave me the error
fatal: not a git repository (or any parent up to mount point /)
-
Toby 1 Kenobi over 2 yearslooks like I needed to
cd
into the cloned repository firt -
Nicko Glayre over 2 years@DavidAmmouial, if you already cloned the upstream repo just do both of the last steps. But I guess if you want to work from your local repo it's just the standard git workflow, doing them locally and pushing them remote, nothing to do with the initial question or do I miss something?
-
David Ammouial over 2 yearsYou're right @NickoGlayre, the original question does mention a local clone.
-
Ed Randall over 2 yearsFinally, a working answer which doesn't require all the remote branches to be created locally, nor a special 'bare' clone, nor a 'mirror' clone
-
BasilBrush over 1 yearIf you want to do it from the website instead of the commandline, github's got an 'import repository' functionality (click on the '+' top right & see dropdown menu), where you can import repos from other websites.
-
Stefano over 1 yearWas getting error
zsh: no matches found: refs/remotes/origin/*:refs/heads/*
until I wrapped the refspec in quotes e.g.""refs/remotes/origin/*:refs/heads/*"
-
Max Carroll over 1 yearcan I confirm that this is copying from origin to neworiginand not the other way round?
-
Max Carroll about 1 yearIn my opinion this is more deserving of up upvotes than any other answer - I am intrigued as to why it hasn't more upvotes