git - clone a single branch only

...and other branches later on

December 12, 2024



Check out a single branch only:

$ git clone --depth=1 --single-branch ssh://git@some_host/project.git project.git
  --single-branch:  Clone only the history leading to the tip of a single branch,[ in this case] specified by the primary branch remote’s HEAD points at[ e.g. 'master' or 'main'].
  --depth:          Create a shallow clone with a history truncated to the specified number of commits.

Right now, obviously, other branches cannot be checked out:

$ cd common.git

$ git checkout -b my-new-feature origin/my-new-feature
fatal: 'origin/my-new-feature' is not a commit and a branch 'my-new-feature' cannot be created from it

Add a single remote branch:

$ git remote set-branches --add origin my-new-feature

Add all branches:

$ git remote set-branches origin "*"

Either way:

$ git fetch
$ git checkout -b my-new-feature origin/my-new-feature