Skip to content

Resolve the canonical drupal.org remote when switching to a non-local branch - #37

Merged
amateescu merged 2 commits into
mainfrom
fix-switch-with-fork-remotes
Jul 30, 2026
Merged

Resolve the canonical drupal.org remote when switching to a non-local branch#37
amateescu merged 2 commits into
mainfrom
fix-switch-with-fork-remotes

Conversation

@amateescu

Copy link
Copy Markdown
Owner

The Issue

How This PR Solves The Issue

A new switch_to_branch helper, used by both the core and the contrib module path, replaces the bare git switch. When the branch is already local it switches as before, and otherwise it resolves a remote itself and switches with --track, which is what removes the ambiguity.

The resolution order:

  • Local branch exists, so plain git switch <branch>, since --track would fail there.
  • The remote whose URL matches project/<name> (drupal for core, the module name for contrib), which is what tells the canonical repository apart from issue forks.
  • checkout.defaultRemote, if the user set it.
  • origin, if it has the branch.
  • The only remote that has the branch, if there is just one.

And when git doesn't know the branch yet, the command says so instead of falling over on fatal: invalid reference: 11.x:

Error: branch '11.x' is not in this checkout, neither locally nor on
any remote you have fetched. If it exists on drupal.org, fetch it first:
  git fetch --all

When several remotes do have it and none of them looks canonical, it lists them and shows how to pick:

Error: several remotes have a branch called '11.x', and none of them
looks like the drupal.org repository for 'drupal':
  drupal-3572173
  drupal-3611989
Pick the one you want:
  git switch --track <remote>/11.x
Or make that choice stick for every ambiguous branch:
  git config checkout.defaultRemote <remote>

Nothing is written to git config, so this also covers existing checkouts, people whose canonical remote is named upstream or drupal, and anyone whose origin is a personal fork, since a fork remote can't win the URL step whatever it is called. The README's core branch switching section now mentions the behaviour.

Manual Testing Instructions

ddev add-on get https://github.com/amateescu/ddev-drupal-dev/tarball/refs/pull/37/head
ddev restart

Automated Testing Overview

Covered by a fake-fork case in the module management test.

@amateescu
amateescu merged commit fd3e02f into main Jul 30, 2026
2 checks passed
@amateescu
amateescu deleted the fix-switch-with-fork-remotes branch July 30, 2026 07:35
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.

Question about using the new command for switching branches

1 participant