Description
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
- Type this
git worktree add ./wt -b feature
cd ./wt
gh repo sync
cd ..
git status
- 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
williammartin commentedon Mar 13, 2026
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
ghand how they interact. Thanks for the report!felmonon commentedon Mar 19, 2026
I dug into this locally and was able to reproduce the underlying Git behavior outside of
ghitself.The current non-current-branch path in
repo syncusesgit 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_HEADappears to already have the safety check we want here. In the same repro,update-refsilently advanced the branch, butbranch -ffailed 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-refpath withgit branch -ffor 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.
txhno commentedon Apr 1, 2026
ive fixed it here: #13075
txhno commentedon Apr 1, 2026
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 labeledhelp wanted.if this fix direction looks acceptable, could a maintainer add the
help wantedlabel to #12927 so PR #13075 can clear triage?fix: avoid corrupting linked worktrees during repo sync
SAY-5 commentedon Apr 17, 2026
Opened #13200 with a fix — adds
IsCheckedOutInOtherWorktreedetection viagit worktree list --porcelainand refuses toupdate-refwith a helpful error when another worktree has the branch. Includes tests. If a maintainer can add thehelp wantedlabel, the PR can proceed.