Skip to content

gh repo sync silently corrupts worktree when synced branch is checked out in a different worktree #12927

Closed

Description

@VitaliVinahradski

Describe the bug

gh repo sync uses git update-ref to fast-forward a branch that is not the current branch. However, it
only checks whether the branch is checked out in the current worktree (currentBranch == branch). It
does not check whether the branch is checked out in another git worktree.

When the synced branch (e.g. main) is checked out in a different worktree, git update-ref advances
the ref without updating that worktree's index or working tree. This leaves the other worktree in a
silently corrupted state.

The relevant code is in pkg/cmd/repo/sync/sync.go, executeLocalRepoSync:

  if currentBranch == branch {
      // uses git merge --ff-only → updates ref + working tree + index ✓
  } else {
      // uses git update-ref → updates ONLY the ref ✗
  }

Affected version

gh version 2.88.1 (2026-03-12)
https://github.com/cli/cli/releases/tag/v2.88.1

Steps to reproduce the behavior

  1. Type this
  git worktree add ./wt -b feature
  cd ./wt
  gh repo sync
  cd ..
  git status
  1. Shows all files changed between old and new main as "staged changes"

Expected vs actual behavior

Expected: gh repo sync detects that main is checked out in another worktree and updates that worktree's index/working tree (e.g. via git -C merge --ff-only)

Actual: Actual: gh repo sync silently runs git update-ref refs/heads/main FETCH_HEAD

Logs

worktree

gh repo sync

main repo

git status

Activity

  1. added
    bug
    on Mar 13, 2026
  2. williammartin commented on Mar 13, 2026

    @williammartin
    Member

    Historically we've just not really done much around worktrees because they didn't have a lot of use, but in this new agentic world, I suspect we're gonna have to revisit a bunch of places in gh and how they interact. Thanks for the report!

  3. added
    gh-repo
    on Mar 13, 2026
  4. added
    priority-2
    and removed
    needs-triage
    on Mar 13, 2026
  5. felmonon commented on Mar 19, 2026

    @felmonon

    I dug into this locally and was able to reproduce the underlying Git behavior outside of gh itself.

    The current non-current-branch path in repo sync uses git update-ref, which advances the ref but does not refresh another worktree that has that branch checked out. In a worktree setup, that leaves the other worktree with the ref moved but the index / working tree still based on the old commit.

    One detail that may simplify the fix: git branch -f <branch> FETCH_HEAD appears to already have the safety check we want here. In the same repro, update-ref silently advanced the branch, but branch -f failed with a fatal error because the branch was already in use by another worktree.

    So instead of adding explicit worktree parsing, would you be open to replacing the existing update-ref path with git branch -f for the non-current existing-branch case?

    That seems like it would let Git enforce the worktree constraint directly, while still keeping the sync behavior simple. If that direction sounds acceptable, I can put together a patch with coverage for the checked-out-in-another-worktree case.

  6. txhno commented on Apr 1, 2026

    @txhno

    ive fixed it here: #13075

  7. txhno commented on Apr 1, 2026

    @txhno

    i updated the PR description with more detail. the remaining blocker is the repository policy in .github/CONTRIBUTING.md: external PRs are only accepted for issues labeled help wanted.

    if this fix direction looks acceptable, could a maintainer add the help wanted label to #12927 so PR #13075 can clear triage?

  8. added a commit that references this issue on Apr 8, 2026
    81e6fb2
  9. SAY-5 commented on Apr 17, 2026

    @SAY-5

    Opened #13200 with a fix — adds IsCheckedOutInOtherWorktree detection via git worktree list --porcelain and refuses to update-ref with a helpful error when another worktree has the branch. Includes tests. If a maintainer can add the help wanted label, the PR can proceed.

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

    buggh-repopriority-2stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions