How to clone a specific branch with git clone -b
Running git clone -b <branch name> <repository> leaves the cloned repository with the specified branch checked out. With a plain clone, your local branch is main (the branch that the source's HEAD points to). If you want to start on a development branch such as develop, or a release branch, you have to switch with git switch after cloning. Adding -b removes that step. I ran the commands in a test environment and include the output from before and after each operation as it appeared.
The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>) and fixed the commit dates with environment variables. The source repository (neko-repo) was in the same test environment, and I cloned it by path. I did not test cloning from a server such as GitHub, and I did not test on Windows or macOS.
The repository used for testing
neko-repo has three branches, main, develop, and release/1.0, and a tag v1.0. The branch that HEAD points to in the source is main.
$ git log --oneline --all --graph --decorate
* 0055a91 (release/1.0) 1.0 の修正
| * 5eda3ea (develop) 開発中の機能
| * ed8e72b (HEAD -> main) main の更新
|/
* 8ee5f47 (tag: v1.0) 最初のコミット
Before cloning, you can check which branches and tags the source has with git ls-remote. --heads limits the output to branches and --tags limits it to tags. The name you pass to -b is the part after refs/heads/ for a branch, or after refs/tags/ for a tag.
$ git ls-remote --heads --tags neko-repo
5eda3ead0b88b7b517ccef3d8e9ec1e6cddf0fff refs/heads/develop
ed8e72bcc3ced6b9e8463f4e901c64449e0e6bc2 refs/heads/main
0055a91bfc131875788bd4f5f705a3e041c547ad refs/heads/release/1.0
8ee5f4721173205d2da5f8e5204156af5b3307d5 refs/tags/v1.0
Cloning without -b
For comparison, I first cloned without -b. The only local branch is main, and the working folder contains only the main file (version.txt). The other branches were fetched as remote-tracking branches (such as origin/develop).
$ git clone neko-repo neko-default
Cloning into 'neko-default'...
done.
$ git -C neko-default branch
* main
$ git -C neko-default branch -r
origin/HEAD -> origin/main
origin/develop
origin/main
origin/release/1.0
$ ls neko-default
version.txt
Cloning develop with -b
I cloned with -b develop. The local branch became develop, and git status showed that it was the same as origin/develop. The working folder also contains develop.txt, which was added on develop.
$ git clone -b develop neko-repo neko-develop
Cloning into 'neko-develop'...
done.
$ cd neko-develop
$ git branch
* develop
$ git status
On branch develop
Your branch is up to date with 'origin/develop'.
nothing to commit, working tree clean
$ git log --oneline
5eda3ea 開発中の機能
ed8e72b main の更新
8ee5f47 最初のコミット
$ ls
develop.txt
version.txt
According to the official git clone reference, -b makes the specified branch the HEAD of the new repository, instead of the branch the source's HEAD points to. --branch works the same way. Names that contain /, such as release/1.0, can be given as they are.
$ git clone -q --branch release/1.0 neko-repo neko-release
$ git -C neko-release branch
* release/1.0
$ git -C neko-release log --oneline
0055a91 1.0 の修正
8ee5f47 最初のコミット
Other branches are still fetched
Even with -b, the clone does not fetch only the specified branch. In neko-develop, all four remote-tracking branches are present, and the fetch setting (remote.origin.fetch) still covers all branches (*). origin/HEAD points to origin/main, matching the source's HEAD.
$ git branch -r
origin/HEAD -> origin/main
origin/develop
origin/main
origin/release/1.0
$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
$ git rev-parse --abbrev-ref origin/HEAD
origin/main
So switching to main later takes only git switch main. There is no local main branch yet, but origin/main exists, so Git creates a main branch that tracks it. According to the official git switch reference, this behavior is called --guess and is enabled by default.
$ git switch main
Switched to a new branch 'main'
branch 'main' set up to track 'origin/main'.
$ git branch
develop
* main
Use –single-branch to fetch only one branch
If you don't need the other branches, add --single-branch to -b. Only origin/develop remains as a remote-tracking branch, and remote.origin.fetch is limited to develop. The official reference says that --single-branch fetches only the history reachable from the specified branch. A later git fetch also updates only that branch.
$ git clone -q -b develop --single-branch neko-repo neko-single
$ git -C neko-single branch -r
origin/develop
$ git -C neko-single config --get remote.origin.fetch
+refs/heads/develop:refs/remotes/origin/develop
$ git -C neko-single log --oneline --all
5eda3ea 開発中の機能
ed8e72b main の更新
8ee5f47 最初のコミット
The commit main の更新 appears in git log --all because it is part of the history of develop. The commit 1.0 の修正, which exists only on release/1.0, was not fetched.
In this state, trying to switch to main fails because origin/main does not exist.
$ git -C neko-single switch main
fatal: invalid reference: main
If you need main later, add it to the branches being fetched with git remote set-branches --add, then run git fetch. This fetched origin/main, and git switch main then worked.
$ git -C neko-single remote set-branches --add origin main
$ git -C neko-single config --get-all remote.origin.fetch
+refs/heads/develop:refs/remotes/origin/develop
+refs/heads/main:refs/remotes/origin/main
$ git -C neko-single fetch -q
$ git -C neko-single branch -r
origin/develop
origin/main
$ git -C neko-single switch main
Switched to a new branch 'main'
branch 'main' set up to track 'origin/main'.
According to the official git clone reference for --depth, --depth implies --single-branch. So when you use --depth 1 to fetch only the latest commit, you get a single branch even without --single-branch.
Passing a tag to -b leaves you with no branch
-b also accepts a tag name instead of a branch name. Cloning with -b v1.0 produced a notice that the repository was in detached HEAD state (not on any branch). git status also shows that no branch is checked out.
$ git clone -b v1.0 neko-repo neko-tag
Cloning into 'neko-tag'...
done.
Note: switching to '8ee5f4721173205d2da5f8e5204156af5b3307d5'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
$ git -C neko-tag status
Not currently on any branch.
nothing to commit, working tree clean
$ git -C neko-tag branch
* (no branch)
$ git -C neko-tag log --oneline --decorate
8ee5f47 (HEAD, tag: v1.0) 最初のコミット
The official reference also says that when a tag is passed to --branch, HEAD is detached at that commit. If you only want to look at the files as they were at the tag, this is fine. To start making changes from there, create a branch with git switch -c as the notice suggests, and then commit.
$ git -C neko-tag switch -c fix-from-1.0
Switched to a new branch 'fix-from-1.0'
$ git -C neko-tag branch
* fix-from-1.0
Error when the name does not exist in the source
When I specified feature, which does not exist in the source, the clone stopped with the error Remote branch feature not found in upstream origin. The destination folder neko-none was not left behind.
$ git clone -b feature neko-repo neko-none
Cloning into 'neko-none'...
fatal: Remote branch feature not found in upstream origin
$ ls -d neko-none
ls: cannot access 'neko-none': No such file or directory
If you see this error, check the branch name with git ls-remote --heads, as shown earlier. Because the folder is not left behind, you do not need to delete anything. You can simply run the command again with the corrected branch name.



