#!/bin/sh
set -e

usage () {
    cat <<'EOF'
usage: git undo-commit [-fh] <file> [<file> ...]

Remove the last commit, but keep its changes staged (like right before the commit).
If you specify the -f flag, it removes the changes too.

Options:
-f    Don't keep the commit's changes (destructive)
-h    Show this help
EOF
}

force=0
while getopts fh flag; do
    case "$flag" in
        f) force=1;;
        h) usage; exit 0;;
    esac
done
shift $(($OPTIND - 1))

if [ $force -eq 1 ]; then
    opts="--hard"
else
    opts="--soft"
fi

git reset $opts HEAD~1
