Skip to content

Instantly share code, notes, and snippets.

@wilmarvh
Last active July 9, 2025 23:31
Show Gist options
  • Save wilmarvh/95fe7daed6ee6a63d811677e040ae421 to your computer and use it in GitHub Desktop.
Save wilmarvh/95fe7daed6ee6a63d811677e040ae421 to your computer and use it in GitHub Desktop.
git checkout-all-branches
#!/bin/bash
#Whenever you clone a repo, you do not clone all of its branches by default.
#If you wish to do so, use the following script:
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master | grep -v main `; do
git branch --track ${branch#remotes/origin/} $branch
done
@bonellimitch
Copy link

Nice!! Works like a charm!

@VM-COD
Copy link

VM-COD commented Oct 14, 2022

Awesome!! Thanks a lot

@MaciekWiniarski
Copy link

You made my day. Thanks a lot!!!

@Mordano
Copy link

Mordano commented May 14, 2024

Nice little script! After using it myself, I encountered a neat little git command which does all of this for you during the git clone step.
You can use git clone --mirror REPO_URL to clone all branches instantly.

@omarjcero
Copy link

Nice little script! After using it myself, I encountered a neat little git command which does all of this for you during the git clone step. You can use git clone --mirror REPO_URL to clone all branches instantly.

It works like a charm! Thanks a lot!

@ronin-tg
Copy link

ronin-tg commented Dec 6, 2024

it`s look like good. I will try it 🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙🤙

@colidyre
Copy link

You probably want to add grep -v main as well.

@wilmarvh
Copy link
Author

wilmarvh commented Mar 3, 2025

You probably want to add grep -v main as well.

Added, thanks!

@cgoldberg
Copy link

cgoldberg commented Jul 9, 2025

Here is a slightly more verbose version that fixes some issues with the original...

  • fetches refs so branches that exist on the remote but not locally are tracked
  • works no matter what your default branch is named (i.e. not just main/master)
  • works even if you have git configured to display branches in color
  • won't display an error if branch already exists locally
git fetch
default_branch=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
for remote_branch in $(git branch --all --no-color | grep remotes | grep -v "${default_branch}"); do
    branch="${remote_branch#remotes/origin/}"
    git branch --track "${branch}" "${remote_branch}" 2>&1 | grep -v "already exists"
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment