Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Hub Clone All Branches At Once

Tags:

git

github

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?

like image 899
sixtyTonneAngel Avatar asked Oct 28 '16 18:10

sixtyTonneAngel


People also ask

How do I clone a git repository with all branches?

The idea is to use the git-clone to clone the repository. This will automatically fetch all the branches and tags in the cloned repository. To check out the specific branch, you can use the git-checkout command to create a local tracking branch.

Does git clone get all remote branches?

git clone downloads all remote branches but still considers them "remote", even though the files are located in your new repository. There's one exception to this, which is that the cloning process creates a local branch called "master" from the remote branch called "master".


2 Answers

(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 fetched all branches to local machine:

enter image description here

like image 198
Do Nhu Vy Avatar answered Sep 23 '22 11:09

Do Nhu Vy


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.

like image 20
Amit Gupta Avatar answered Sep 19 '22 11:09

Amit Gupta