Skip to content

Instantly share code, notes, and snippets.

@szhu
Last active January 14, 2022 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szhu/8848e8b8760ceff4f306a98676156c6e to your computer and use it in GitHub Desktop.
Save szhu/8848e8b8760ceff4f306a98676156c6e to your computer and use it in GitHub Desktop.
Creates a backup of your uncommitted changes.
#!/bin/bash
set -e
old_stash_count=$(git stash list | wc -l)
git stash push --keep-index --include-untracked --quiet
if test "$old_stash_count" == "$(git stash list | wc -l)"; then
echo "All changes have been committed."
exit 0
fi
if test "$(git rev-parse 'autosave':)" == "$(git rev-parse 'stash@{0}':)"; then
echo "Tree is identical to last autosave."
else
git tag -f "$(date +'autosave-%Y%m-%d%H%M')" 'stash@{0}'
git branch -f 'autosave' 'stash@{0}'
echo -n "Autosaved at: "
git rev-parse --short=7 'stash@{0}'
fi
git stash pop --quiet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment