Re: Git Stash Synchronization - Best Workflow?
From: Brooke Kuhlmann <hidden>
Date: 2025-09-11 02:22:48
Hey Phillip
I'm unable to reproduce this. In the script below the final push succeeds.
That's because you need to export every time before you push. Like this: touch one.txt git stash push --include-untracked --message "One" git stash export --to-ref "refs/stashes/$USER" git push --no-verify --force-with-lease --force-if-includes origin "refs/stashes/$USER" git stash pop git stash push --include-untracked --message "One II" git stash export --to-ref "refs/stashes/$USER" git push --no-verify --force-with-lease --force-if-includes origin "refs/stashes/$USER" The above will yield the following error: ! [rejected] refs/stashes/bkuhlmann -> refs/stashes/bkuhlmann (remote ref updated since checkout) error: failed to push some refs to 'https://github.com/bkuhlmann/test' hint: Updates were rejected because the tip of the remote-tracking branch has hint: been updated since the last checkout. If you want to integrate the hint: remote changes, use 'git pull' before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. However, if you perform the above with only the single "git stash export" then you won't get the error as you discovered in your workflow. The only way I've been able to make this work is to do this: touch one.txt git stash push --include-untracked --message "One" git stash export --to-ref "refs/stashes/$USER" git push --no-verify --force origin "refs/stashes/$USER" git stash pop git stash push --include-untracked --message "One II" git stash export --to-ref "refs/stashes/$USER" git push --no-verify --force origin "refs/stashes/$USER" Notice that I always export before the push AND that I'm using `--force` each time. That's the only way to ensure your local stash is in sync with the remote stash. You can always verify that the remote stash is being updated by always clearing your local stash and then immediately importing to check if your stash message was updated properly. Example: git stash clear git stash import "refs/stashes/$USER" git stash list Once you perform the import, and immediately list what's in your stash, you should see something similar to the following: stash@{0} 6ba4eaea3751 On main: One II When your remote stash isn't updated, you'll see this: stash@{0} 6ba4eaea3751 On main: One (Notice the difference between the message of "One" versus "One II")
You can force the creation of a reflog
I tried that too which makes the error go away but doesn't update the remote stash at all. Example: touch one.txt git stash push --include-untracked --message "One" git stash export --to-ref "refs/stashes/$USER" git push --no-verify --force-with-lease --force-if-includes origin "refs/stashes/$USER" git stash pop git stash push --include-untracked --message "One II" oid=$(git rev-parse --verify refs/stashes/$USER) && git update-ref -d refs/stashes/$USER && git update-ref --create-reflog -m 'export stashes' refs/stashes/$USER $oid git push --no-verify --force-with-lease --force-if-includes origin "refs/stashes/$USER" The above works but if I run `git stash clear && git stash import "refs/stashes/$USER"`, I find that my local stash doesn't have the message change (still using "One" instead of "One II" which means the remote stash never got updated). Sadly, I can only seem to make this work when using a force push but would definitely be nice to not have to use a force push.
set -ex dir="$(mktemp -d)" cd "$dir" git init --bare origin git init repo cd repo git remote add origin "file://${PWD%/*}/origin" git config core.logAllRefUpdates always git config remote.origin.fetch refs/stashes/*:refs/remote/origin/stashes/* echo a >a git add a git commit -m a echo b >a git stash push echo c >a git stash push git stash export --to-ref refs/stashes/test git push origin refs/stashes/test git stash pop git stash push -m message git stash export --to-ref refs/stashes/test git push --force-with-lease --force-if-includes origin refs/stashes/testquoted
quoted
You need to pass the name of the ref whose reflog you want to look at, otherwise it defaults to showing the reflog for HEAD. You should be able to see the reflog for you exported stashes.>I gave this a try and every time I use `git reflog refs/stashes/$USER`, I always get a blank response. No errors and no output.Ah, I wonder if core.logAllRefUpdates only affects the creation of new refs. You can force the creation of a reflog by running oid=$(git rev-parse --verify refs/stashes/$USER) && git update-ref -d refs/stashes/$USER && git update-ref --create-reflog -m 'export stashes' refs/stashes/$USER $oid the same applies to refs/remote/stashes/origin/$USERquoted
quoted
Let's try and find why the remote update say's it rejected when it isn't and then we can think about the best way to document pushing and pulling exported stashes.I haven't thought much about the pulling side of this. "git stash import" appends to the existing stashes so I'm not sure how we'd cope with forced updates - have got got any experience of handling this from your experiments? Thanks Phillip