Subject: Re: Git Stash Synchronization - Best Workflow?

From: Phillip Wood <[email-masked]>
Date: Mon, 1 Sep 2025 11:10:02 +0100

Hi Brooke

[I've cc'd brian to see what he thinks about setting up a reflog by 
default when exporting stashes]

On 01/09/2025 00:25, Brooke Kuhlmann wrote:
> Hello.
> 
> When using Git 2.51.0, what is the correct way to safely export
 > your stash and then keep that stash up-to-date? Here's an example
 > workflow:>
> touch demo.txt
> 
> git stash push --include-untracked --message "Demo"
> git stash export --to-ref "refs/stashes/$USER"
> git push origin "refs/stashes/$USER"
> 
> git stash pop stash@{0}
> git push origin "refs/stashes/$USER"

This push doesn't do anything because refs/stashes/$USER is unchanged 
since the last push

> git stash push --include-untracked --message "Demo II"
> git stash export --to-ref "refs/stashes/$USER"
> git push origin "refs/stashes/$USER"

This push fails because you've popped and then pushed a stash since the 
last export so refs/stashes/$USER on the remote cannot fast-forward
> Notice, in the middle, I pop the stash only to rename it. Upon 
 > pushing these changes back up, I get the following error:>
> To https://github.com/bkuhlmann/test
>   ! [rejected]                  refs/stashes/bkuhlmann -> refs/stashes/bkuhlmann (non-fast-forward)
> error: failed to push some refs to 'https://github.com/bkuhlmann/test'
> hint: Updates were rejected because a pushed branch tip is behind its remote
> hint: counterpart. If you want to integrate the remote changes, use 'git pull'
> hint: before pushing again.
> hint: See the 'Note about fast-forwards' in 'git push --help' for details.
> 
> The work around is to use `git push --force` when pushing updates.
 > I'd like to use `git push --force-with-lease` but that doesn't work.
You can use --force-with-lease=refs/stashes/$USER:$expect where $expect 
is the value of refs/stashes/$USER when you last pushed. The problem is 
that there is no easy way to find that as by default refs/stashes/$USER 
does not have a reflog and there is no remote tracking ref set up for it 
either. If you add a fetch refspec like

     refs/stashes/*:refs/remote/origin/stashes/*

(note "remote" rather than "remotes" to avoid clashing with the default 
refspec for branches) then refs/remote/origin/stashes/$USER should be 
updated when you push to or pull from refs/stashes/* and I think a bare 
--force-with-lease will work. In general --force-with-lease without 
explicitly specifying $expect is not that safe as it will happily 
overwrite the remote ref if you fetch and do not incorporate the remote 
changes into your local changes before pushing. Using 
--force-if-includes is safer if you don't want to give $expect 
explicitly. That requires a reflog for the local ref though which you 
can enable by setting core.logAllrefUpdates=always. We should perhaps 
change the export code to create a reflog for the ref we're exporting 
the stashes to and maybe expand the documentation to mention setting up 
a fetch refspec.

Thanks

Phillip

> I realize that force pushing over your remote stash makes a lot more sense since you typically never share a stash with folks but was thinking it would be nice to ensure you don't accidentally override your remote stash when working on different machine when you forgot to import first. Basically, wanting to protect myself from myself. :)
> 
> Is force push the only way to handle this use case or is there a better approach? Thanks!