Skip to content

Instantly share code, notes, and snippets.

@mgilliam
Created October 22, 2011 06:23
Show Gist options
  • Save mgilliam/1305709 to your computer and use it in GitHub Desktop.
Save mgilliam/1305709 to your computer and use it in GitHub Desktop.
Clone a git repository and create a local tracking branch for each remote branch.
#!/bin/bash
#
# Clone a git repository and create a local tracking branch for each remote
# branch, optionally disconnecting from the remote.
usage=$(
cat <<EOF
$0 [-d] <repo>
-d Disconnect from the remote when finished.
EOF
)
# Defaults:
disconnect_default=
repo_default=""
while getopts "d" OPTION ; do
case "$OPTION" in
d)
d=true
;;
esac
done
: ${d=$disconnect_default}
: ${target=$repo_default}
for last; do true; done
target=$last
if [ -z $target ] ; then
echo "$usage"
exit 1
fi
git clone "$target"
cd `echo "$target" | sed -e 's/^.*\///' -e 's/\..*$//'` >/dev/null 2>&1
echo -e "\nCreating local branches...\n"
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`;
do
git branch --track ${branch##*/} $branch
done
if [ $d ] ; then
git remote rm origin
echo -e "\nDisconnected this working copy from the remote."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment