Git Hub Clone All Branches At Once

匿名 (未验证) 提交于 2019-12-03 02:45:02

问题:

I'm trying to clone an entire repository onto my machine using linux. I used

git clone <url>    

I then went into the folder where it was downloaded and typed

git branch  

in the terminal. It's only showing me master and not other branches which were in the remote repository. How do I clone all branches?

I know that for each branch in the remote I can separately use

git checkout -b <name of local branch> origin/<name of remote branch> 

but is there any way other than that?

回答1:

(1) Inside git local repostitory, create a new sh file

touch getAllBranches.sh vi getAllBranches.sh 

(2) Insert the below content to getAllBranches.sh file:

for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do    git branch --track ${branch#remotes/origin/} $branch done 

(3) Get all branches:

chmod +x getAllBranches.sh     sh getAllBranches.sh 

(4) Check result at local repository:

git branch 

For example, I use repository: https://github.com/donhuvy/spring-boot

As you can see, I have been fetched all branches to local machine:



回答2:

This isn't too much complicated, very simple and straight forward steps are as follows:

After cloning the repo, run $ cd myproject

git branch -a This will show you all the remote branches.

$ git branch -a * master   remotes/origin/HEAD   remotes/origin/master   remotes/origin/v1.0-stable   remotes/origin/experimental 

If you want to work on remote branch, you'll need to create a local tracking branch:

$ git checkout -b experimental origin/experimental 

Verify whether you are in the desired branch by the following command;

$ git branch 

The output will like this;

*experimental master some branch2 some branch3  

Notice the * sign that denotes the current branch.



回答3:

It's only showing me master and not other branches which were in the remote repository. How do I clone all branches?

Branches are essentially pointers to commits. When you do a git clone (or a git fetch), you retrieve all of the commits from the remote repository, and all of its branches as well.

However, git branch does not show remote branches by default. Instead, it shows you your local branches, which may or may not have any relation to branches that exist on the remote. If you run git branch --all, git will report all of the branches it knows about, both local and remote.

It's worth noting that tags do not operate this way, and there is no distinction between a local and remote tag.



回答4:

git clone --bare <repository url goes here> .git 

Then after the repo is clone with all its branches then do the following

git config --bool core.bare false  git reset --hard 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!