DEV Community

Nelson Figueroa
Nelson Figueroa

Posted on • Originally published at nelson.cloud on

How to Clone a Specific Git Branch Without Other Branches

Cloning a Specific Branch

To clone a specific branch of a git repository without cloning all other branches, use the following command formula:

git clone --single-branch --branch <branch_name> <repo_URL.git>
Enter fullscreen mode Exit fullscreen mode

For example, if you want to clone the release-1.28 branch of the Kubernetes GitHub repository, run:

git clone --single-branch --branch release-1.28 https://github.com/kubernetes/kubernetes.git
Enter fullscreen mode Exit fullscreen mode

Cloning the Latest Commit of a Specific Branch

If you only want to clone the latest commit of a specific branch (which results in a faster and smaller cloning operation) use --depth 1. The command formula looks like this:

git clone --single-branch --branch <branch_name> --depth 1 <repo_URL.git>
Enter fullscreen mode Exit fullscreen mode

And here is another example using the release-1.28 branch of the Kubernetes GitHub repository:

git clone --single-branch --branch release-1.28 --depth 1 https://github.com/kubernetes/kubernetes.git
Enter fullscreen mode Exit fullscreen mode

References

Top comments (0)