How can I make Git stash with untracked files, push it to patch and restore it in another computer.
git stash save -u feature
git stash show -p > patch
git apply patch
But path hasn't untracked files
How can I make Git stash with untracked files, push it to patch and restore it in another computer.
git stash save -u feature
git stash show -p > patch
git apply patch
But path hasn't untracked files
A normal git stash
creates a stash bag that consists of two commits: the index, and the work-tree.
Using -u
or -a
(or their other spellings) creates a three-commit stash bag. The third commit contains (only) the untracked (-u
) or untracked-and-ignored / "all" (-a
) files, i.e., it omits all tracked files.
If you need this in the form of a patch, the trick is to create two patches:
git stash show -p
(what you have so far), plusThe easiest way to get the second bullet item is to git diff
that third commit against an empty tree. Git always has an empty tree in every repository whose ID is the magic number 4b825dc642cb6eb9a060e54bf8d69288fbee4904
. So a diff between that tree, and stash^3
, will consist of a series of git patches that add the untracked files:
git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 stash^3
You can then simply combine the two patches into one:
git stash show -p > patch
git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 stash^3 >> patch
(See the last link above for a way to avoid hard-coding the magic number for the empty tree. Also, if you just want to view the u
commit, use git show
: git show stash^3
, for instance.)
git stash save --all
but when I do git diff stash^3
zsh: no matches found: stash^3 If I understand right I need to make these three steps ? git stash save --all
git stash show -p > patch
git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 stash^3 >> patch
–
Inequitable ^3
itself, instead of passing it on to git. You must protect the ^
from zsh, with backslash or quotes, e.g., 'stash^3'
instead of just stash^3
. (Different shells have different Special Characters, sh and bash don't need this.) –
Judaist git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 stash^3 >> patch
I get patch but also contains .gitignored files ? –
Inequitable --all
rather than --untracked
. You can restore and drop that stash and make one with just -u
if that's what you want instead. –
Judaist git hash-object -t tree /dev/null
). –
Judaist git apply
: git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 stash^3 [<path spec>] | git apply -
–
Detergent date +'%Y-%m-%d'_%H:%M:%S
"; git stash show -p >${patchName}; git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 stash^3 >>${patchName}; echo "${patchName}" –
Solecism © 2022 - 2024 — McMap. All rights reserved.
git stash show -p stash@{0} > patch
to create the patch file. – Barbarossagit stash show
just diffs the work-tree commit against its parent. – Judaist