How to fetch all Git branches

2019-01-01 09:32发布

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

I know that I can do git branch -a to see all the branches, but how would I pull all the branches locally so when I do git branch, it shows the following?

$ git branch
* master
* staging
* etc...

24条回答
十年一品温如言
2楼-- · 2019-01-01 09:52

Just those 3 commands will get all the branches

git clone --mirror repo.git  .git     (gets just .git  - bare repository)

git config --bool core.bare false         

git reset --hard                  
查看更多
流年柔荑漫光年
3楼-- · 2019-01-01 09:52

If you have problems with fetch --all

then track your remote branch

git checkout --track origin/%branchname%

查看更多
忆尘夕之涩
4楼-- · 2019-01-01 09:55

After you clone the master repository, you just can execute

git fetch && git checkout <branchname>
查看更多
琉璃瓶的回忆
5楼-- · 2019-01-01 09:57

You can fetch one branch from all remotes like this:

git fetch --all

fetch updates local copies of remote branches so this is always safe for your local branches BUT:

  1. fetch will not update local branches (which track remote branches); If you want to update your local branches you still need to pull every branch.

  2. fetch will not create local branches (which track remote branches), you have to do this manually. If you want to list all remote branches: git branch -a

To update local branches which track remote branches:

git pull --all

However, this can be still insufficient. It will work only for your local branches which track remote branches. To track all remote branches execute this oneliner BEFORE git pull --all:

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

TL;DR version

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

(it seems that pull fetches all branches from all remotes, but I always fetch first just to be sure)

Run the first command only if there are remote branches on the server that aren't tracked by your local branches.

P.S. AFAIK git fetch --all and git remote update are equivalent.



Kamil Szot's comment, which 74 (at least) people found useful.

I had to use:

for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done

because your code created local branches named origin/branchname and I was getting "refname 'origin/branchname' is ambiguous whenever I referred to it.

查看更多
何处买醉
6楼-- · 2019-01-01 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.

查看更多
若你有天会懂
7楼-- · 2019-01-01 09:57

Based on the answer by Learath2, here's what I did after doing git clone [...] and cd-ing into the created directory:

git branch -r | grep -v master | awk {print\$1} | sed 's/^origin\/\(.*\)$/\1 &/' | xargs -n2 git checkout -b

Worked for me but I can't know it'll work for you. Be careful.

查看更多
登录 后发表回答