Skip to content

fix(git-merge): fetch upstream before merging a local branch#280

Merged
fdaviddpt merged 1 commit into
masterfrom
fix/git-merge-fetch-before-merge
Jun 7, 2026
Merged

fix(git-merge): fetch upstream before merging a local branch#280
fdaviddpt merged 1 commit into
masterfrom
fix/git-merge-fetch-before-merge

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Context

git-merge's docstring promised "fetch (optional) + merge" but the code never fetched. So git-merge master merged the local master ref — stale the moment origin moves ahead.

Real bite this session: a timeout fix had landed on origin/master, but merging master into a feature branch silently used the lagging local ref. The fix "wasn't there" and the failing CI job kept failing — with no signal that the merge was a no-op against an old base.

Fix

New _fresh_merge_ref():

  • Resolves the ref's upstream and git fetches it before merging.
  • If the local branch is behind its upstream, redirects the merge to the upstream (e.g. origin/master) with a loud note: local master was N behind origin/master — merging origin/master (latest) instead.
  • Offline / no-upstream / fetch-fail → warns and falls back to the local ref (no hard failure).
  • Remote-tracking refs (origin/master) are refreshed directly.

Tests

Two real-repo tests (bare remote + stale clone, no mocking):

  • test_merge_redirects_to_upstream_when_local_stale — advances origin via a second clone, asserts the merge pulls the upstream commit.
  • test_merge_local_only_branch_no_fetch — a branch with no upstream merges as-is.

Full git/merge suite: 376 passed, 1 skipped.

🤖 Generated by Max — the stale-merge fix, fittingly, almost shipped stale.

git-merge's docstring promised "fetch + merge" but never fetched, so
`git-merge master` merged the stale local ref. When origin moved ahead,
the merge silently missed the new commits — a fix that had landed on
origin/master "wasn't there" and a failing job kept failing.

_fresh_merge_ref() now resolves the ref's upstream, fetches it, and if
the local branch is behind, merges the upstream (latest) instead with a
loud note. Offline / no-upstream / fetch-fail cases warn and fall back to
the local ref. Remote-tracking refs are refreshed directly.

Two real-repo tests (bare remote + stale clone) cover the redirect and
the local-only fallback.

Co-Authored-By: Max <noreply>
@fdaviddpt fdaviddpt merged commit 796166c into master Jun 7, 2026
12 checks passed
@fdaviddpt fdaviddpt deleted the fix/git-merge-fetch-before-merge branch June 7, 2026 16:28
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.

1 participant