git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Git Stash Synchronization - Best Workflow?

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Sep 1, 2025, 10:10 UTC
Message-ID
<5dee5f49-eeb6-49e2-8bca-6ae6a1d6be5d@gmail.com>
In-Reply-To
<7B1CCA36-23F1-410D-84ED-6E965989EA8B@alchemists.io>
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:>
Show 8 quoted lines
> 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:>
Show 9 quoted lines
> 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!
Previous: Brooke KuhlmannNext: Brooke Kuhlmann
Message 2 of 9 in “Git Stash Synchronization - Best Workflow?”
  1. Brooke KuhlmannAug 31, 2025
  2. Phillip WoodSep 1, 2025
  3. Brooke KuhlmannSep 1, 2025
  4. Phillip WoodSep 5, 2025
  5. Brooke KuhlmannSep 6, 2025
  6. Phillip WoodSep 10, 2025
  7. Brooke KuhlmannSep 11, 2025
  8. Phillip WoodSep 19, 2025
  9. Brooke KuhlmannSep 19, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.