How to fetch all Git branches

后端 未结 30 987
情书的邮戳
情书的邮戳 2020-11-22 09:38

I cloned a Git repository, which contains about five branches. However, when I do git branch I only see one of them:

$ git branch
* master
         


        
30条回答
  •  囚心锁ツ
    2020-11-22 09:57

    Here's something I'd consider robust:

    • Doesn't update remote tracking for existing branches
    • Doesn't try to update HEAD to track origin/HEAD
    • Allows remotes named other than origin
    • Properly shell quoted
    for b in $(git branch -r --format='%(refname:short)'); do
      [[ "${b#*/}" = HEAD ]] && continue
      git show-ref -q --heads "${b#*/}" || git branch --track "${b#*/}" "$b";
    done
    git pull --all
    

    It's not necessary to git fetch --all as passing -all to git pull passes this option to the internal fetch.

    Credit to this answer.

提交回复
热议问题