How to clone all remote branches in Git?

后端 未结 30 2096
情话喂你
情话喂你 2020-11-22 01:08

I have a master and a development branch, both pushed to GitHub. I\'ve cloned, pulled, and fetched, but I re

30条回答
  •  你的背包
    2020-11-22 01:41

    This Bash script helped me out:

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

    It will create tracking branches for all remote branches, except master (which you probably got from the original clone command). I think you might still need to do a

    git fetch --all
    git pull --all
    

    to be sure.

    One liner: 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
    As usual: test in your setup before copying rm -rf universe as we know it

    Credits for one-liner go to user cfi

提交回复
热议问题