Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone another branch after cloning single-branch

I'm using the below command to clone one branch:

git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder

Now I want to check out another branch from the server. I tried the below command and it didn't work

git checkout another_branch

After cloning a single branch, how can I clone/checkout/pull/fetch another branch?

like image 345
Satyam Avatar asked Sep 08 '25 03:09

Satyam


2 Answers

Besides Mureinik's answer—which is good for some "one-off" / short-term work cases—you can also use git remote to add additional branches, or update your single-branch clone to an all-branch clone:

git remote set-branches --add origin another-branch

After this, git fetch origin will create the remote-tracking name origin/another-branch, which will allow git checkout another-branch to invoke the --guess mode to create your (local) branch name another-branch from your remote-tracking name origin/another-branch.

To de-single-branch-ize a clone, use:

git remote set-branches origin "*"

(followed by git fetch as usual).

Note that whether you need to quote the asterisk depends on your command-line-interpreter, but in general it's safe to do it.

like image 89
torek Avatar answered Sep 10 '25 05:09

torek


You can fetch another remote branch by specifying it after the remote name in a git fetch call:

git fetch origin another_branch

Once it's fetched, you'll find it in the FETCH_HEAD, and an use that to create a local branch:

git checkout FETCH_HEAD -b another_branch
like image 25
Mureinik Avatar answered Sep 10 '25 04:09

Mureinik