Git stashes for ADHDers
If you are like me you probably typed more than once git stash
and meant git stash list
while working on many different things at the same time. If not, then feel free to go somewhere else and don’t waste your time here :]
I, however, thought, wouldn’t it be nice if git stash
would show me a list of existing stashes instead of telling me that, once again, I have “No local changes to save”.
A while back I wrote about my git status
hook system. This can be used as a starter for an extension of the git stash
command.
The idea is simple: I want to see a list of stashes when I type git stash
.
1git() {
2 if [[ $# -eq 0 ]]; then
3 command git
4 return
5 fi
6 FILE=.git/hooks/status
7 case "$1" in
8 status)
9 [[ -x $FILE ]] && bash $FILE
10 command git "$@"
11 ;;
12 stash)
13 if [[ $# -eq 1 || ( "$2" == "-m" && $# -ge 3 ) ]]; then
14 local stash_args=()
15 if [[ "$2" == "-m" ]]; then
16 stash_args=("--include-untracked" "$2" "$3")
17 for ((i=4; i<=$#; i++)); do
18 stash_args+=("${!i}")
19 done
20 else
21 stash_args=("--include-untracked")
22 fi
23 output=$(command git stash "${stash_args[@]}" 2>&1)
24 echo "$output"
25 if echo "$output" | grep -q "No local changes to save"; then
26 echo -e "\nCurrent stashes"
27 command git stash list
28 fi
29 else
30 command git "$@"
31 fi
32 ;;
33 *)
34 command git "$@"
35 ;;
36 esac
37}
The magic happens in lines 12 to 31. Everything else is explained in my previous post. This extension checks if the stash command comes with any additional parameters and executes them if so. If not, it checks if there are any local changes to save and if not, it shows the list of available stashes.
Works nice for me.
One thing to note: I prefer to --include-untracked
when stashing. If you don’t, you might want to remove it from the script. This will add all untracked files to the stash.
It’s a hack, but for now it works.
This post is part of #100DaysToOffload 49 posts since Apr 28, 2023 — 101 total posts