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:59

I believe you have clone the repository by

git clone https://github.com/pathOfrepository

now go to that folder using cd

cd pathOfrepository

if you type git status

you can see all

   On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

to see all hidden branch type

 git branch -a

it will list all the remote branch

now if you want to checkout on any particular branch just type

git checkout -b localBranchName origin/RemteBranchName
查看更多
笑指拈花
3楼-- · 2019-01-01 10:00

Use git fetch && git checkout RemoteBranchName.

It works very well for me...

查看更多
路过你的时光
4楼-- · 2019-01-01 10:01
$ git remote update
$ git pull --all

This assumes all branches are tracked.

If they aren't you can fire this in Bash:

for remote in `git branch -r `; do git branch --track $remote; done

Then run the command.

查看更多
呛了眼睛熬了心
5楼-- · 2019-01-01 10:01

Make sure all the remote branches are fetchable in .git/config file.

In this example, only the origin/production branch is fetchable, even if you try to do git fetch --all nothing will happen but fetching the production branch:

[origin]
fetch = +refs/heads/production:refs/remotes/origin/production

This line should be replaced by:

[origin]
fetch = +refs/heads/*:refs/remotes/origin/*

Then run git fetch etc...

查看更多
看风景的人
6楼-- · 2019-01-01 10:03

I usually use nothing else but commands like this:

git fetch origin
git checkout --track origin/remote-branch

A little shorter version:

git fetch origin
git checkout -t origin/remote-branch
查看更多
永恒的永恒
7楼-- · 2019-01-01 10:03

Here's a Perl version of the one-liner provided in the accepted answer:

git branch -r | perl -e 'while(<>) {chop; my $remote = $_; my ($local) = ($remote =~ /origin\/(.*)/); print "git branch --track $local $remote\n";}' > some-output-file

You can run the output file as a Shell script if you'd like.

We deleted our Stash project repository by accident. Fortunately someone had created a fork right before the accidental loss. I cloned the fork to my local (will omit the details of how I did that). Once I had the fork fully in my local, I ran one one-liner. I modified the remote's URL (origin in my case) to point to the target repository we were recovering to:

git remote set-url origin <remote-url>

And finally pushed all branches to origin like so:

git push --all origin

and we were back in business.

查看更多
登录 后发表回答