Skip to content

GIT - Fetching remote branch on shallow clones

When working on repositories that have a lot of branches, often it is more benificial to only clone the branches that you are interested in, for this you would use:

> git clone -branch new_feature git://remoterepository.git

So, later on you need another branch from the remote repository, you can edit the file .git/config and add a line to the remote section like so:

fetch = +refs/heads/newer_feature:refs/remotes/origin/newer_feature

Where you would have the branch name newer_feature and the origin as you need.

You end up with something like this:

  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true
  symlinks = true
[remote "origin"]
  url = [email protected]
  fetch = +refs/heads/new_feature:refs/remotes/origin/new_feature
  fetch = +refs/heads/newer_feature:refs/remotes/origin/newer_feature
  fetch = +refs/heads/main:refs/remotes/origin/main
[branch "new_feature"]
  remote = origin
  merge = refs/heads/new_feature
  email = [email protected]

Then you can get the remote:

> git fetch origin/newer_feature

or just

> git fetch origin