Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MrZenW/e4588ef9c5f9ac284350e78f7c157974 to your computer and use it in GitHub Desktop.
Save MrZenW/e4588ef9c5f9ac284350e78f7c157974 to your computer and use it in GitHub Desktop.

Git clone all branches and push to a different remote

- 0x00 Initialisation -

Step 0x01 - add your own remote

# add another remote
git remote add mrzenw_github __ANOTHER_REMOTE_GIT_ADDRESS__HTTPS_OR_SSH__

Step 0x02 - create local branches as same as them in the origin repository

# checkout all branches
# https://stackoverflow.com/a/10312587
# fetch and pull
git fetch --all
git pull --all
git branch -r | grep -v '\->' | grep 'origin/' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

Step 0x03 - push to your own remote

# push all branches from `origin` to `mrzenw_github` remote
git branch -r | grep -v '\->' | grep 'origin/' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git push -u mrzenw_github "${remote#origin/}"; done

Step all-in-one

# add another remote
git remote add mrzenw_github __ANOTHER_REMOTE_GIT_ADDRESS__HTTPS_OR_SSH__

# checkout all branches
# https://stackoverflow.com/a/10312587
# fetch and pull
git fetch --all
git pull --all
git branch -r | grep -v '\->' | grep 'origin/' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

# push all branches from `origin` to `mrzenw_github` remote
git branch -r | grep -v '\->' | grep 'origin/' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git push -u mrzenw_github "${remote#origin/}"; done

- 0x10 Update -

Step 0x11 - set the origin upstream back to the original remote

# fetch and pull
git fetch --all
git pull --all
# set back upstream remote to `origin`
git branch -r | grep -v '\->' | grep 'origin/' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch -u origin "${remote#origin/}"; done

Step 0x12 - create local branches as same as them in the origin repository

# checkout all branches
# https://stackoverflow.com/a/10312587
git branch -r | grep -v '\->' | grep 'origin/' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

Step 0x13 - push to your own remote

# push all branches from `origin` to `mrzenw_github` remote
git branch -r | grep -v '\->' | grep 'origin/' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git push -u mrzenw_github "${remote#origin/}"; done

Step all-in-one

# fetch and pull
git fetch --all
git pull --all
# set back upstream remote to `origin`
git branch -r | grep -v '\->' | grep 'origin/' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch -u origin "${remote#origin/}"; done

# checkout all branches
# https://stackoverflow.com/a/10312587
git branch -r | grep -v '\->' | grep 'origin/' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

# push all branches from `origin` to `mrzenw_github` remote
git branch -r | grep -v '\->' | grep 'origin/' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git push -u mrzenw_github "${remote#origin/}"; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment