Skip to content

Instantly share code, notes, and snippets.

@jtzero
Created May 13, 2024 15:10
Show Gist options
  • Save jtzero/66128357267126dd4bf9e9b7ffd68dd5 to your computer and use it in GitHub Desktop.
Save jtzero/66128357267126dd4bf9e9b7ffd68dd5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -Eeuo pipefail
COMMIT_SHA="${1:-}"
printerr() { printf "\033[0;31m%s\033[0m\n" "$@" >&2; }
usage() {
# shellcheck disable=SC2016
printerr 'usage: git-autosquash $COMMIT_SHA
COMMIT_SHA defaults to a "fixup!"~1 commit
use '\''git commit --fixup $COMMIT_SHA'\'' before running'
}
if [ -z "${COMMIT_SHA}" ]; then
usage
exit 2
else
if [ "$(git rev-list --abbrev-commit --max-parents=0 HEAD)" = "${COMMIT_SHA}" ]; then
GIT_SEQUENCE_EDITOR=: git rebase -i --root --autosquash
else
GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash "${COMMIT_SHA}~1"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment