If you are moving all branches to a new repo from an old one then in your local repo you need to set up tracking of each branch to existing origin branches, before pushing to the new repo, otherwise all your origin branches won’t appear in the new origin. Do this manually by tracking or checking out each branch, or use the one liner:
for remote in `git branch -r | grep -v '\->' | grep -v master`; do git branch --track `echo $remote|sed 's=origin/=='` `echo $remote`; done
This one line command is based on versions of it in other answers on this page, but is arguably better because:
- it correctly sets up the branch tracking, unlike some older variants of this command on this page which only supply one parameter to --track and thus each branch ends up tracking master - not good
- names the local branches without the prefix “origin/” which I personally don’t want - and is consistent with what happens when you checkout a branch normally.
- skips tracking master since that is already happening
- doesn’t actually checkout anything thus is fast
- avoids stumbling over the -> in the output of git branch -r
Next, if you are switching origins, replace the link to the old origin and point to a new remote. Ensure you create the new remote first, using bitbucket/github GUI, but don’t add any files to it or there will be a merge problem. E.g.
git remote set-url origin [email protected]:YOUR/SOMEREPO.git
Now push. Note the second command is needed to push the tags as well:
git push -u --all origin
git push --tags origin