When Git Cannot See a Remote Branch

When Git Cannot See a Remote Branch: origin/dev Not Found

While working on a Git repository, I encountered a situation where Git could not see a remote branch even though it clearly existed on the remote server.

The error message was:

fatal: invalid reference: origin/dev

At first glance this looks strange. If the branch exists remotely, Git should be able to track it locally. The issue turned out to be related to how Git fetches remote branches.

Verifying That the Remote Branch Exists

The first step was to check whether the branch actually existed on the remote repository:

git ls-remote --heads origin

Example output:

670b975870485f20dc6d793f922848b2382df294  refs/heads/dev
be714f052262f6b7bc080492f24dd8e3a9ab410d  refs/heads/main

This confirms that the dev branch exists on the remote repository.

However, locally the branch was not visible:

git branch -r

The output was empty.

Trying to switch to the branch failed:

git switch -c dev origin/dev
fatal: invalid reference: origin/dev

Understanding the Cause

To understand what was happening, I inspected the configuration of the remote repository:

git remote show origin

The output showed that only the main branch was being tracked:

Remote branch:
  main tracked

This means the repository was configured to fetch only the main branch. As a consequence, git fetch never created the remote-tracking reference origin/dev.

In other words, the branch existed on the server but Git was not downloading it locally.

Fixing the Fetch Configuration

The solution is to restore the standard fetch rule so that Git downloads all remote branches.

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

Then fetch again:

git fetch origin --prune

Git now downloads the missing branch:

[new branch]      dev -> origin/dev

Remote branches are now visible:

git branch -r
origin/dev
origin/main

Finally, create the local branch and link it to the remote one:

git switch -c dev origin/dev
branch 'dev' set up to track 'origin/dev'

Key Takeaway

  • git ls-remote queries the remote repository directly.
  • git fetch creates local remote-tracking branches according to the configured fetch refspec.

If the fetch configuration is restricted, some remote branches may exist on the server but will never appear locally.

Useful Command

To check what your repository fetches from the remote:

git config --get-all remote.origin.fetch

The standard configuration is:

+refs/heads/*:refs/remotes/origin/*

This rule tells Git to fetch all branches from the remote repository.

Comments