Git clone, but a specific branch, please

This is a super quick post mainly to remind myself that it is possible to (git) clone a specific branch, rather than the default branch. And it’s actually quite simple to do so.

As per git-clone (1), the command:

Clones a repository into a newly created directory, creates remote-tracking branches for each branch in the cloned repository (visible using git branch --remotes), and creates and checks out an initial branch that is forked from the cloned repository’s currently active branch.

For most of us, the currently active branch is going to be main.

Defaults using git clone

Let’s put theory into practice using my Java Blogposts repository.

$ git clone git@github.com:martincarstenbach/java-blogposts.git
Cloning into 'java-blogposts'...
remote: Enumerating objects: 359, done.
remote: Counting objects: 100% (97/97), done.
remote: Compressing objects: 100% (73/73), done.
remote: Total 359 (delta 14), reused 67 (delta 10), pack-reused 262 (from 1)
Receiving objects: 100% (359/359), 83.19 KiB | 439.00 KiB/s, done.
Resolving deltas: 100% (82/82), done.

So, as expected, the repository is now present on disk. What about these branches?

$ cd java-blogposts && git --no-pager branch -va
* main                                     0e4eb66 Merge pull request #10 from martincarstenbach/devcontainers
  remotes/origin/HEAD                      -> origin/main
  remotes/origin/main                      0e4eb66 Merge pull request #10 from martincarstenbach/devcontainers
  remotes/origin/springboot-thymeleaf-demo 3fe5197 renamed file to allow GitHub to render it when entering the directory
  remotes/origin/unittest                  304fe12 feat: adding sample app featuring Liquibase and JUnit5

The current branch is set to main (which is tracking origin/main), as visible in the output of the following command:

$ git --no-pager branch -vv
* main 0e4eb66 [origin/main] Merge pull request #10 from martincarstenbach/devcontainers

This matches the documentation perfectly.

Let’s check a specific branch out

But what if I wanted to check out a specific branch, and without any additional steps? Here’s what you do. Remember from the above output that there’s a branch named unittest. I want to check this branch out immediately, without having to switch to it and setting the remote tracking branch.

$ git clone --branch unittest git@github.com:martincarstenbach/java-blogposts.git
Cloning into 'java-blogposts'...
remote: Enumerating objects: 359, done.
remote: Counting objects: 100% (97/97), done.
remote: Compressing objects: 100% (73/73), done.
remote: Total 359 (delta 14), reused 67 (delta 10), pack-reused 262 (from 1)
Receiving objects: 100% (359/359), 83.19 KiB | 887.00 KiB/s, done.
Resolving deltas: 100% (82/82), done.

That’s literally it. Just say git clone --branch <your remote branch name here> and you’re golden.

$ git status 
On branch unittest
Your branch is up to date with 'origin/unittest'.

nothing to commit, working tree clean

$ git --no-pager branch -vv
* unittest 304fe12 [origin/unittest] feat: adding sample app featuring Liquibase and JUnit5

Happy developing!