Skip to content

Clone all branches and tags at once

Following script clone all #git branches and tags at once:

#!/bin/bash
git fetch --all --tags
for branch in `git branch -r --format="%(refname:short)" | sed 's/origin\///'`
do git branch -f --track "$branch" "origin/$branch"
done

Single line version:

Terminal window
git fetch --all --tags; for branch in `git branch -r --format="%(refname:short)" | sed 's/origin\///'`; do git branch -f --track "$branch" "origin/$branch" ; done ;

Motivation?

This command is super usefull when you need copy repo between remotes:

Terminal window
git remote add alternative git@github.com:....git

Check if is there another remote:

Terminal window
git remote -v
> origin git@github.com:.../....git (fetch)
> origin git@github.com:.../....git (push)
> alternative git@github.com:.../....git (fetch)
> alternative git@github.com:.../....git (push)

Push all changes to alternative remote

Terminal window
git push alternative --all # push all braneches
git push alternative --tags # push all tags