Created
June 22, 2026 10:56
-
-
Save ninjadynamics/fbba9e47ff43b029c10c61e0d6c41fb3 to your computer and use it in GitHub Desktop.
Git Checkpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # install-git-checkpoint.sh | |
| # | |
| # Installs a global Git alias: | |
| # | |
| # git checkpoint "message" | |
| # git checkpoint list | |
| # git checkpoint apply | |
| # git checkpoint apply 2 | |
| # git checkpoint pop | |
| # git checkpoint pop 2 | |
| # | |
| # What it does: | |
| # | |
| # git checkpoint "add matrix code" | |
| # | |
| # Creates a named stash: | |
| # | |
| # checkpoint: add matrix code | |
| # | |
| # Then immediately reapplies it, so your working tree stays dirty and you can | |
| # keep working. This gives you a lightweight checkpoint without making a commit. | |
| # | |
| # Notes: | |
| # | |
| # - Uses git stash push -u, so untracked files are included. | |
| # - apply restores a checkpoint but keeps it in the stash list. | |
| # - pop restores a checkpoint and removes it if it applies cleanly. | |
| # - apply/pop with no number defaults to stash@{0}. | |
| # - apply/pop with a number maps to stash@{N}. | |
| # | |
| # Remove the alias later with: | |
| # | |
| # git config --global --unset alias.checkpoint | |
| # | |
| # Check the installed alias with: | |
| # | |
| # git config --global --get alias.checkpoint | |
| git config --global alias.checkpoint '!f() { | |
| ref() { | |
| case "$1" in | |
| stash@\{*\}) printf "%s\n" "$1" ;; | |
| "") printf "%s\n" "stash@{0}" ;; | |
| *) printf "%s\n" "stash@{$1}" ;; | |
| esac | |
| } | |
| case "$1" in | |
| list) | |
| git stash list | grep "checkpoint:" || true | |
| ;; | |
| apply) | |
| shift | |
| git stash apply --index "$(ref "$1")" | |
| ;; | |
| pop) | |
| shift | |
| git stash pop --index "$(ref "$1")" | |
| ;; | |
| help|--help|-h) | |
| echo "Usage:" | |
| echo " git checkpoint \"message\"" | |
| echo " git checkpoint list" | |
| echo " git checkpoint apply [N|stash@{N}]" | |
| echo " git checkpoint pop [N|stash@{N}]" | |
| ;; | |
| *) | |
| if [ -z "$(git status --porcelain)" ]; then | |
| echo "No changes to checkpoint." | |
| exit 0 | |
| fi | |
| msg="${*:-manual checkpoint}" | |
| git stash push -u -m "checkpoint: $msg" && | |
| git stash apply --index stash@{0} | |
| ;; | |
| esac | |
| }; f' | |
| echo "Installed git checkpoint alias." | |
| echo | |
| echo "Try:" | |
| echo " git checkpoint \"add matrix code\"" | |
| echo " git checkpoint list" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment