Skip to content

fix: resolve branch upstream in shallow clones via git config fallback#304980

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/shallow-clone-upstream
Open

fix: resolve branch upstream in shallow clones via git config fallback#304980
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/shallow-clone-upstream

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

When a repository is cloned with git clone --depth 1 (shallow clone), the branch's upstream property is undefined. This is because git for-each-ref with %(upstream:short), %(upstream:track), %(upstream:remotename), and %(upstream:remoteref) format fields returns empty values in shallow clones, even when the branch tracking configuration exists in .git/config.

This causes multiple issues:

  • The status bar doesn't show upstream tracking information
  • Sync operations may not work correctly
  • Extensions like GitHub Pull Requests can't detect the upstream branch

Closes #248328

What is the new behavior?

After parsing for-each-ref output, if the upstream is still undefined for a local branch, the code now falls back to reading the branch tracking configuration directly using:

  • git config --get branch.<name>.remote
  • git config --get branch.<name>.merge

This correctly resolves the upstream even in shallow clones where for-each-ref upstream fields are empty.

Additional context

Root cause was identified by @lszomoru in the issue comments. The for-each-ref command with upstream format fields relies on local reference data that is not available in shallow clones. However, the branch tracking config (branch.<name>.remote and branch.<name>.merge) is always written by git clone regardless of depth, making it a reliable fallback source.

When a repository is cloned with --depth 1, git for-each-ref returns
empty upstream fields because the tracking info is not fully available
locally. This causes the branch upstream to be undefined, breaking
features like sync, status bar upstream info, and PR creation.

Add a fallback that reads branch.<name>.remote and branch.<name>.merge
from git config when for-each-ref returns no upstream information.

Closes microsoft#248328
@vs-code-engineering vs-code-engineering bot added this to the 1.114.0 milestone Mar 26, 2026
@alexr00 alexr00 removed this from the 1.114.0 milestone Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Git extension API branch upstream is undefined when repo was cloned shallow

3 participants