Sarathlal N

Clone all branches in a remote Git repository

One of the remote git repository has master, development & some other branches. I’ve cloned, pulled, and fetched, but I remain unable to get anything other than the master branch. I need to get all branches from remote.

Here is the solution.

Bash script

#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
    git branch --track "${branch##*/}" "$branch"
done

One line command

git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs

Recent Posts

  1. Change / Disable the Easy Digital Download renewal discount if the license key expired
  2. Create dynamic image using Python with Pillow package
  3. Discard local changes in Git
  4. Remove last commit from Git repository
  5. Change the directory name of Git repository

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.