Skip to content

Instantly share code, notes, and snippets.

@superfawkes
Created June 30, 2017 04:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superfawkes/810131f0cce203695ce72993b8f2ece9 to your computer and use it in GitHub Desktop.
Save superfawkes/810131f0cce203695ce72993b8f2ece9 to your computer and use it in GitHub Desktop.
Interactive Git Stash utility
#!/bin/bash -f
function RecalcStashes {
PS3="`git stash list | wc -l` stashes present. Enter Option > "
}
function List {
if [[ $1 -eq 2 ]]; then
git stash list --pretty=format:"%C(red)%h%C(reset) - %C(green)(%cr) %C(bold blue)<%an>%C(reset) off %C(red)%p: %+s"
else
git stash list --pretty=format:"%C(red)%h%C(reset) - %gd:%<(70,trunc)%s %C(green)(%cr) %C(bold blue)<%an>%C(reset)"
fi
}
function Save {
prompt=$( [[ $1 -eq 2 ]] && echo "Save unstaged as: " || echo "Save all changes as: ")
echo -n $prompt
read saveName
if [[ $1 -eq 2 ]]; then
git stash save --keep-index $saveName
else
git stash save $saveName
fi
RecalcStashes
}
function Pop {
echo -n "Pop which stash index? "
read idx
git stash pop "stash@{$idx}"
RecalcStashes
}
function Drop {
echo -n "Discard which stash index? "
read idx
git stash drop "stash@{$idx}"
RecalcStashes
}
function Apply {
echo -n "Discard which stash index? "
read idx
git stash apply "stash@{$idx}"
RecalcStashes
}
function Rename {
echo -n "Rename which index? "
read idx
git stash-rename "stash@{$idx}" || echo "you need the stash-rename config shortcut for this - see inside script" && exit
# Execute this to create config:
# git config --global alias.stash-rename '!_() { rev=$(git rev-parse $1) && git stash drop $1 || exit 1 ; git stash store -m "$2" $rev; }; _'
# Uses this shortcut... http://stackoverflow.com/a/25935360/134793
# the alternative is to catch the output, use awk to get value and parse it.
RecalcStashes
}
echo -e "\n=== Git Stash Utility ===\n"
options=("list" "list-detail" "save" "save-unstaged" "pop" "discard" "apply" "rename" "help")
reshow=1
RecalcStashes
while (( reshow )); do
select opt in "${options[@]}" "quit"; do
case $REPLY in
1|2 ) List "$REPLY";;
3|4 ) Save `echo $REPLY - 2 |bc -l`;;
5 ) Pop;;
6 ) Drop;;
7 ) Apply;;
8 ) Rename;;
9 ) echo "Not much. Google git stash basics. Enter the menu-option's number (or q to quit). Each sub-option is interactive in its own way."; break;;
$(( ${#options[@]} + 1 )) ) reshow=0; break;;
'q' ) reshow=0; break;;
*) echo "Not recognised. Enter the option-index - or 'q' to quit:"; break;;
esac
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment