Skip to content

gh repo sync can update a branch checked out in another worktree #15121

Open

Description

@brechtcox-be

Describe the bug

When gh repo sync updates a non-current local branch, it checks for associated worktrees only after trying to update the branch. On Git 2.34.1, that update can succeed even if the branch is checked out in another worktree. Sync then reports success and the other worktree can be left with stale files and index state.

Affected version

gh version 2.104.0-50-ge1219499a (2026-11-18)

Steps to reproduce the behavior

  1. Type this: go test ./...
  2. These tests will fail:
  • TestExecuteLocalRepoSyncNonCurrentBranch/branch_checked_out_in_another_worktree
  • TestExecuteLocalRepoSyncNonCurrentBranch/branch_associated_with_prunable_worktree

Expected vs actual behavior

Expected behavior

When the target branch is checked out in another worktree, gh sync repo should refuse to update it and explain how to sync safely.

Before updating the ref, gh repo sync should detect whether the branch is checked out in another worktree (including stale/prunable worktree metadata), refuse to update it and show an actionable message. Syncing a branch not associated with another worktree should continue to work.

Actual behavior

gh sync repo can report success and move the target even though it is checked out in another worktree. that worktree's files and index remain at the old commit, leaving them out of sync with the branch.

Logs

--- FAIL: TestExecuteLocalRepoSyncNonCurrentBranch (0.65s)
    --- FAIL: TestExecuteLocalRepoSyncNonCurrentBranch/branch_checked_out_in_another_worktree (0.18s)
        sync_test.go:143: 
            	Error Trace:	github-cli/pkg/cmd/repo/sync/sync_test.go:143
            	Error:      	An error is expected but got nil.
            	Test:       	TestExecuteLocalRepoSyncNonCurrentBranch/branch_checked_out_in_another_worktree
    --- FAIL: TestExecuteLocalRepoSyncNonCurrentBranch/branch_associated_with_prunable_worktree (0.13s)
        sync_test.go:169: 
            	Error Trace:	github-cli/pkg/cmd/repo/sync/sync_test.go:169
            	Error:      	An error is expected but got nil.
            	Test:       	TestExecuteLocalRepoSyncNonCurrentBranch/branch_associated_with_prunable_worktree
FAIL

Activity

  1. cli-triage commented on Nov 21, 2026

    @cli-triage

    I believe this is a duplicate of #12927 because it tracks the same gh repo sync failure where advancing a branch checked out in another worktree leaves that worktree's index and files stale.

    Generated by issue triage.

  2. added
    duplicate
    on Nov 21, 2026
  3. brechtcox-be commented on Nov 21, 2026

    @brechtcox-be
    Author

    This is marked as a duplicate. However the issue that is being referred to #12927 as duplicate is closed although the bug is still happening.

    I have a fix for this and can open a PR for it but the issue should first be labeled help wanted.

  4. sergiou87 commented on Nov 23, 2026

    @sergiou87
    Contributor

    According to Copilot…

    Verdict: Valid bug, not duplicate of closed [#12927](#12927). Affects Git < 2.35.0 (including Ubuntu 22.04 LTS default Git 2.34.1).

    Repro (isolated git repo)

    • Git 2.56.1 (local): git branch --force -- trunk FETCH_HEAD exits 128 (fatal: cannot force update the branch 'trunk' used by worktree at ...). TestExecuteLocalRepoSyncNonCurrentBranch passes.
    • Git 2.34.1 (built in temp dir, cleaned up after):
      • git branch --force -- trunk FETCH_HEAD exits 0, moves trunk ref to FETCH_HEAD, leaves linked worktree file.txt at "old" and git status --porcelain dirty (M file.txt).
      • git worktree list --porcelain on 2.34.1 does emit branch refs/heads/trunk and prunable gitdir file points to non-existent location (git/client.go:parseWorktrees handles both).
      • Running go test ./pkg/cmd/repo/sync -run TestExecuteLocalRepoSyncNonCurrentBranch with Git 2.34.1 reproduces exact failure from [#15121](gh repo sync can update a branch checked out in another worktree #15121): branch_checked_out_in_another_worktree and branch_associated_with_prunable_worktree both fail with An error is expected but got nil.

    Root cause

    1. Git < 2.35.0: Before git/git commit 593a2a5d06 (branch: protect branches checked out in all worktrees, shipped in Git 2.35.0), validate_new_branchname() in branch.c only checked current worktree HEAD — git branch --force silently allowed moving branches checked out in other worktrees.
    2. pkg/cmd/repo/sync/sync.go:261-273: Commit 4463cad66 ([#14076](Curate the error when the sync target is checked out in another worktree #14076)) checked git worktree list --porcelain before UpdateBranch. Commit 28ecebfa0 ([#14060](Prevent repo sync from corrupting linked worktrees #14060)) moved git.Worktrees() inside if err := git.UpdateBranch(branch, "FETCH_HEAD"); err != nil. On Git < 2.35.0, UpdateBranch returns nil, so worktree check never runs. (pkg/cmd/pr/merge/merge.go still checks Worktrees() upfront.)

    Fix: Check git.Worktrees() before git.UpdateBranch, keep git branch --force as mutation-time safety net on Git >= 2.35.0. Remove duplicate label on [#15121](#15121).

  5. added
    bug
    priority-2
    and removed
    duplicate
    on Nov 23, 2026
  6. removed
    needs-triage
    on Nov 23, 2026
  7. added
    gh-repo
    on Nov 23, 2026
  8. brechtcox-be commented on Nov 23, 2026

    @brechtcox-be
    Author

    I'm willing to open a PR for a solution but I'm not allowed to without this issue having the help wanted label.

  9. added
    auto-bugfix-ineligible
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    auto-bugfix-ineligiblebuggh-repopriority-2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions