Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to export stash as a file, and apply it to another computer

Stash current changes

  • git > Stash > Stash (Include Untracked)

Create stash as patch

git stash show "stash@{0}" -p > changes.patch

Apply patch

git apply changes.patch
Copy link

nicovv44 commented Nov 9, 2021

Thanks 😁

Copy link

I also just used this and found it helpful. It shows up as the first search engine result for the query "export git stash", for me.

Thank you for sharing it!

Copy link

@jioo I need to export also in the same file untracked files that were stashed along with the tracked ones, can you help me with this, I can show them with
git show stash@{0}^3
But I do not know how to include them along the tracked and stashed ones

Copy link

Got it, just in case this help:
1- Save staged changes without include untracked changes
git --no-pager stash show stash@{1} -p > patch_1
2- Save staged changes only untracked changes
git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 stash@{1}^3 >> patch_1(diff against an empty tree object)
3- Apply staged save in patch_1 fixing trailing whitespaces
git apply --reject --whitespace=fix patch_1

Alternative for step 2 recommended in case git changes its hash algorithm:
git diff $(git hash-object -t tree /dev/null) stash@{1}^3 >> patch_1

4b825dc642cb6eb9a060e54bf8d69288fbee4904 -> Git always has an empty tree in every repository whose ID is the magic number 4b825dc642cb6eb9a060e54bf8d69288fbee4904
stash@{1}^3 -> Untracked Files that were staged
printf "tree 0\0" | sha1sum
git hash-object -t tree /dev/null
git diff $(printf "tree 0\0" | sha1sum | awk '{print $1}') stash@{1}^3 >> patch_1


git apply --reject --whitespace=fix patch_1
--whitespace=fix ensures whitespace errors are fixed before path is applied
--reject ensures atomicity (so no working directory files are modified if patch will not apply)

Source: out for *.rej files with --reject param)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment