Git - Named stashes
Git aliases to save and pop stashes by name
Here are some handy Git aliases I am using to save and pop stashes by name:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[alias]
# save a stash with a custom name
stash-save = "!f() { \
if [ -z \"$*\" ]; then \
echo \"Error: stash name required\" >&2; \
return 1; \
fi; \
git stash push -m \"$*\"; \
}; f"
# pop a stash by its name/message (uses first stash matching partial name)
stash-pop = "!f() { \
if [ -z \"$*\" ]; then \
echo \"Error: stash name required\" >&2; \
return 1; \
fi; \
ref=$(git stash list | grep -F \"$*\" | cut -d: -f1 | head -n1); \
if [ -n \"$ref\" ]; then \
git stash pop \"$ref\"; \
else \
echo \"Error: no matching stash found\" >&2; \
return 1; \
fi; \
}; f"
Note: requires a shell with compatible tools: grep/cut/head
Example usage:
save/pop a stash by full name:
1
2
3
4
5
$ git stash-save My cool stash
Saved working directory and index state On main: My cool stash
$ git stash list
stash@{0}: On main: My cool stash
$ git stash-pop My cool stash
save/pop a stash by partial name:
1
2
3
$ git stash-save FooBarBaz
Saved working directory and index state On main: FooBarBaz
$ git stash-pop Foo
save stash with no name:
1
2
$ git stash-save
Error: stash name required
pop stash with invalid name:
1
2
$ g stash-pop Wrong name
Error: no matching stash found
This post is licensed under CC BY 4.0 by the author.