Skip to content

Copilot CLI uses global git stash in worktrees #1725

@jmpatri615

Description

@jmpatri615

Describe the bug

Summary

When running multiple Copilot CLI agents in separate Git worktrees of the same repository, the agents use git stash under the hood. Because refs/stash is global to the repository, stashes created by one agent/worktree are visible to all others. As a result, another agent can inadvertently stash pop the wrong entry, causing cross‑worktree contamination (applying or dropping a different agent’s stash). This breaks the expected isolation of background sessions and can lead to data loss or corrupted workspace state. (Worktrees explicitly share refs and history; only HEAD and the index are per‑worktree.)

Background / Rationale

In Git, all worktrees share the same repository object database and refs; only certain files are worktree‑local (e.g., HEAD, index). The stash lives at refs/stash, so the stash list is shared across worktrees. [git-scm.com], [devtoolbox.dedyn.io]
VS Code’s background‑agent guidance emphasizes using worktrees to isolate changes; however, using global stash inside those worktrees undermines that isolation when agents run in parallel. [code.visua...studio.com]

Special Note

The mechanism used by GitHub Copilot CLI to resolve its stashing issues should not interfere with a user using git stash as it works out of the box/standard git stash management workflows.

Affected version

GitHub Copilot CLI 0.0.419.

Steps to reproduce the behavior

From a repo, create two worktrees A and B (e.g., git worktree add ../wt-A and git worktree add ../wt-B ). [git-scm.com]
Start Copilot CLI in both folders (A and B) and instruct each agent to make edits that trigger its internal use of git stash (e.g., when switching tasks/branches or preparing patches).
Agent in worktree A performs git stash (implicitly via Copilot).
Agent in worktree B later runs git stash pop (implicitly via Copilot).
Observe that B applies/drops A’s stash, not B’s own, because both sessions share the single refs/stash stack.

Expected behavior

Each Copilot CLI session operating in a different worktree should have stash‑like isolation, so that temporary saves/restores of working changes cannot collide across worktrees.
Alternatively, Copilot CLI should avoid global stash entirely when multiple sessions might run concurrently within the same repository.

Additional context

Environment

Tool: GitHub Copilot CLI (latest at time of filing)
Context: Multiple concurrent Copilot CLI background sessions, each operating in its own git worktree directory for the same repo
Git: recent (worktrees enabled)
OS: reproducible on macOS and Linux (likely OS‑agnostic)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions