Step by step on how to checkout all branches of a remote origin and push them all onto a new origin.

  1. cd into your repo

  2. save the following script under your repo’s path:

    1
    2
    3
    4
    5
    6
    #!/bin/zsh
    for branch in `git branch -a | grep remotes/origin`; do
    # echo ${branch:15:${#branch}}
    # echo "origin/"${branch:15:${#branch}}
    git branch --track ${branch:15:${#branch}} "origin/"${branch:15:${#branch}}
    done

    Be sure to comment out the echos to see the prints on terminal before executing the real track command!! All they did was to sub and concat strings return by the git branch command. You may have to change the starting and ending indexes, or modify the prefix on target origin branch.

    It will track all the remote branches to your machine in the following format:

    origin branch name: remotes/origin/develop

    local branch name: develop

    command execute:

    1
    git branch --track develop origin/develop
  3. under repo path, run

    1
    git fetch --all

    to update all branches to the latest on your machine.

    If you have GUI apps like SourceTree on your machine, check and see whether all the branches are latest. If there are still badges telling you there are new commits to pull, pull them before you go on to the next step.

  4. create new origin :

    1
    git remote add <new-origin-name> <new-origin-repo-url>

    example:

    1
    git remote add my-awesome-new-origin http://new-origin-host.com/new-repo.git
  5. push all local branches to new origin:

    1
    git push --all my-awesome-new-origin
  6. All done!


References:

[1] git clone all remote branches locally
[2] How to fetch all git branches
[3] Push local Git repo to new remote including all branches and tags