From: Phillip Wood Date: Wed, 26 Aug 2026 10:08:55 GMT Subject: Re: [RFC] stash: let the stash stack live in a configurable ref Message-ID: <4c7734f5-2ef4-4bb0-9397-a97cd67a3bd4@gmail.com> In-Reply-To: Hi Vladimir On 25/08/2026 19:24, Vladimir Sitnikov wrote: > Kristoffer Haugsbakk writes: > >> To my uninformed, not-using-agents mind, what agents do with the tool >> seems like the least concerning thing. They can be non-deterministically >> instructed to not use the stash. > [...] >> And for determinism you can give them a git(1) wrapper that bans >> git-stash(1). > > A wrapper that bans git-stash(1) misses it, because a command that never > mentions the stash still writes to refs/stash: > > git rebase topic --autostash > Created autostash: 637ca99 > Applying autostash resulted in conflicts. > Your changes are safe in the stash. > You can run "git stash pop" or "git stash drop" at any time. > > The entry sits in refs/stash afterwards, where every worktree sees it. > rebase.autoStash lives in the shared .git/config, so one worktree can > turn this on for all of them, and the flag then never appears in any > command line to ban. > > Having a stash subcommand and forbidding it to execute is fishy. > Agents might miss the instructions and they might still attempt calling stash. Right, your wrapper would need to be a bit more complicated. It would need to pass "-c rebase.autoStash=false -c merge.autoStash=false -c pull.autoStash=false", disallow command-lines containing --autostash and probably set GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true to enforce that. > Your point about the bar for a configuration option is well taken, and > so is the one about the framing. The single confused session justifies > the option on its own; the agents changed how often it happens, not > whether it is worth fixing. > > Phillip Wood writes: > >> When I'm not trying to move changes between branches I associate a stash >> with the branch that's checked out when it is created, not the worktree >> where that the branch happens to be checked out. We already record the >> branch name when creating the stash so perhaps we should add an option >> to pop the last stash that was created on the current branch. > > That covers more of my case than I expected. Git refuses to check the > same branch out in two worktrees, so a per-branch filter separates every > worktree that sits on a branch, and the entries stay visible to > `git stash list`, which a private ref gives up. > > The gap is a detached HEAD: > > git checkout --detach > echo v2 >f && git stash push -m "my own text" > git stash list > stash@{0}: On (no branch): my own text > > Every detached worktree records the same "(no branch)", so a per-branch > filter groups them together rather than separating them. That is not a > corner case for the tools I have in mind: one repository here has 436 > linked worktrees, 23 of them on a detached HEAD, because a worktree > created to look at someone else's commit has no branch to be on. When we create a stash entry its first parent is HEAD. So I think you can find the last stash that was created in the current worktree by using HEAD's reflog and the first parents of the stashes. You want to find the most recent stash entry stash@{M} where object-id(HEAD@{N}) == object-id(refs/stash@{M}^1) && reflog-date(HEAD@{N}) <= commit-date(refs/stash@{M}) && commit-date(refs/stash@{M}) <= reflog-date{HEAD@{N-1}) that will give you the stash whose first parent is the same commit as HEAD in the current worktree when the stash was created. >> If an agent really needs a private stash it can use "git stash create" >> and record the oid of the stash under "refs/worktree/". > > That is what I do today, and it works. It is also what every tool has > to reimplement, and the ones that skip it produce the failure I opened > with. > > The two ideas may well be one knob. If the scope of the stack were > configurable (the shared ref, the current branch, or a per-worktree > ref), then "pop what this branch stashed" and "pop what this worktree > stashed" become two values rather than two features, and a detached > worktree can choose the one that still separates it. > > I am happy to write the patch for whichever direction you prefer, or to > test yours. My preference would be to add options to "git stash pop" to pop the most recent stash created on the current branch, or the most recent stash created in the current worktree. That keeps the convenience of the global store for moving changes between different branches/worktrees while making it easy to pop the stash that you intended. It would be nice to have options to filter the output of "git stash list" by branch or worktree as well but that could be added separately. I'd be interested to hear what others think Thanks Phillip