Clone another branch after cloning single-branch

Viewed 364

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?

2 Answers

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

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.

Related