# How to fetch all remote branches in Git after cloning only a single branch

Often I clone only a single commit from one branch of a git repository so that the download will be quick and I can start installing the project. However, after getting the project set up I often want to be able to pull other branches and commits as well. In this post I will show you how to update your git repository to be able to pull other branches and commits after cloning a single branch and commit.

This is how I would normally clone the last commit from a single branch:

git clone <remote-url> --branch <branch-name> --depth 1 
1

To enable fetching all branches from the remote, we can update the remote's fetch Refspec (opens new window). The Refspec is what tells git what branches to look for in the remote. We can get our remote's current Refspec like this:

git config remote.origin.fetch
1

The output will be something like:

+refs/heads/<branch-name>:refs/remotes/origin/<branch-name>
1

We can update the Refspec to enable fetching all branches like this:

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" 
1

To fetch all of the remote branches, we now need to run:

git remote update
1

To pull all of the old commits on the branch that we initially cloned, we can run:

git fetch --unshallow
1

We can also use the --depth argument (opens new window) instead to limit the number of commits to fetch. If we only want to fetch the last 10 commits, we can run:

git fetch --depth 10
1

Newsletter

If you'd like to subscribe to my blog, please enter your details below. You can unsubscribe at any time.

Powered by Buttondown.

Last Updated: 11/20/2023, 10:04:51 AM