Skip to content

Instantly share code, notes, and snippets.

@devdrops
Created November 18, 2016 22:05
Show Gist options
  • Select an option

  • Save devdrops/ca068dd3e3623e7318607b8178a0d1cf to your computer and use it in GitHub Desktop.

Select an option

Save devdrops/ca068dd3e3623e7318607b8178a0d1cf to your computer and use it in GitHub Desktop.
Git: checkout all remote branches locally
#!/bin/bash
# First things first
current=`git branch | grep \* | cut -d ' ' -f2`
git fetch --all
# Once we know all the remote branches
remoteprefix='remotes/'
branchprefix='remotes/origin/'
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
# Removing 'remotes/'
remoteandbranch=${branch#$remoteprefix}
# Removing 'remotes/origin/'
localbranch=${branch#$branchprefix}
# Remote name
remote=${remoteandbranch%\/$localbranch}
# Removing 'remotes/origin/'
localbranch=${branch#$branchprefix}
# Remote only
remote=${remoteandbranch%\/$localbranch}
git checkout -b $localbranch $remote/$localbranch
done
# Back to the current branch
git checkout $current
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment