#!/usr/bin/env bash
# git-undo-demo.sh -- reproduces every transcript in the ankurm.com post
# "Git Undo Last Commit". Run: bash git-undo-demo.sh
# Written and tested with git 2.43.0. It uses "git init -b" and GIT_CONFIG_GLOBAL, so use a recent git.
# Everything happens in a temporary directory that is deleted on exit; your own repos and config are untouched.
# Dates are pinned, so your commit hashes should match the ones in the post.
set -u
ROOT=$(mktemp -d); trap 'rm -rf "$ROOT"' EXIT; export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null
export GIT_AUTHOR_NAME=Demo GIT_AUTHOR_EMAIL=demo@example.com GIT_COMMITTER_NAME=Demo GIT_COMMITTER_EMAIL=demo@example.com
export GIT_EDITOR=true GIT_AUTHOR_DATE="2026-01-01T00:00:00+0000" GIT_COMMITTER_DATE="2026-01-01T00:00:00+0000"
step() { printf '\n##### %s\n' "$1"; }
run()  { printf '$ %s\n' "$*"; eval "$@" 2>&1; }
fresh() { rm -rf "$ROOT/r"; mkdir "$ROOT/r"; cd "$ROOT/r"; git init -q -b main
  echo "one" > a.txt; git add a.txt; git commit -q -m "Add search endpoint"
  echo "two" >> a.txt; git commit -q -am "Fix pagination bug"
  echo "debug=true" > debug.cfg; echo "three" >> a.txt; git add -A; git commit -q -m "Add debug logging (oops)"; }

step "01-start"; fresh; run git log --oneline; run git status -sb

step "02-soft"; fresh; run git reset --soft HEAD~1; run git status -sb; run git log --oneline
step "03-mixed"; fresh; run git reset HEAD~1; run git status -sb; run git log --oneline
step "04-hard"; fresh; echo "untracked" > notes.txt; run git reset --hard HEAD~1; run git status -sb; run git log --oneline
step "05-reflog-recovery"; fresh; run git reset --hard HEAD~1; run git reflog; run 'git reset --hard HEAD@{1}'; run git log --oneline
step "06-orig-head"; fresh; run git reset --hard HEAD~1; run git rev-parse --short ORIG_HEAD; run git reset --hard ORIG_HEAD; run git log --oneline -1
step "07-amend-message"; fresh; run 'git commit --amend -m "Add request logging"'; run git log --oneline
step "08-amend-forgotten-file"; fresh; echo "x" > forgot.txt; git add forgot.txt; run git commit --amend --no-edit; run git show --stat --oneline HEAD
step "09-first-commit"; rm -rf "$ROOT/f"; mkdir "$ROOT/f"; cd "$ROOT/f"; git init -q -b main; echo x > f; git add f; git commit -q -m "first"
run git reset --soft HEAD~1; run git update-ref -d HEAD; run git status -sb
step "10-revert"; fresh; run git revert --no-edit HEAD; run git log --oneline; run git status -sb
step "11-revert-middle-conflict"; fresh; echo "four" >> a.txt; git commit -q -am "Add feature X"; run git revert --no-edit HEAD~1; run git status -sb; run git revert --abort; run git status -sb
step "12-revert-middle-clean"; fresh; echo "x" > x.txt; git add x.txt; git commit -q -m "Add feature X"; run git revert --no-edit HEAD~1; run git log --oneline
step "13-revert-merge"; fresh; git checkout -q -b feature; echo f > feat.txt; git add feat.txt; git commit -q -m "feature work"; git checkout -q main; git merge -q --no-ff feature -m "Merge feature"
run git revert --no-edit HEAD; run git revert --no-edit -m 1 HEAD; run git log --oneline --graph; run git merge feature
step "14-hard-eats-uncommitted"; fresh; echo "my unsaved work" >> a.txt; run git status -sb; run git reset --hard HEAD~1; run git status -sb; run 'cat a.txt'
step "15-keep"; fresh; echo "my unsaved work" >> a.txt; run git reset --keep HEAD~1; run git status -sb
step "16-keep-ok"; rm -rf "$ROOT/k"; mkdir "$ROOT/k"; cd "$ROOT/k"; git init -q -b main; echo a > a.txt; echo b > b.txt; git add .; git commit -q -m "Add files"
echo "bad" >> b.txt; git commit -q -am "Bad change to b.txt"; echo "my unsaved work" >> a.txt; run git status -sb; run git reset --keep HEAD~1; run git status -sb; run 'cat a.txt'
setup_remote() { rm -rf "$ROOT/remote.git" "$ROOT/me" "$ROOT/mate"; git init -q --bare -b main "$ROOT/remote.git"
  cd "$ROOT"; git clone -q remote.git me 2>/dev/null; cd me; git remote set-url origin ../remote.git
  echo a > a; git add a; git commit -q -m "base"; echo bad > bad; git add bad; git commit -q -m "Bad commit"; git push -q origin main 2>/dev/null
  cd "$ROOT"; git clone -q remote.git mate 2>/dev/null; cd mate; git remote set-url origin ../remote.git; }
step "17-lease-clean"; setup_remote; cd "$ROOT/me"; git reset -q --hard HEAD~1
run git status -sb; run git push --force-with-lease origin main; run git log --oneline origin/main
step "18-lease-stale"; setup_remote; (cd "$ROOT/mate"; echo c > c; git add c; git commit -q -m "Teammate work"; git push -q origin main 2>/dev/null)
cd "$ROOT/me"; git reset -q --hard HEAD~1
run git push --force-with-lease origin main
run git fetch origin; run git log --oneline origin/main
step "19-plain-force-destroys"; setup_remote; (cd "$ROOT/mate"; echo c > c; git add c; git commit -q -m "Teammate work"; git push -q origin main 2>/dev/null)
cd "$ROOT/me"; git reset -q --hard HEAD~1
run git push --force origin main; run 'git --git-dir=../remote.git log --oneline main'
step "20-plain-push-rejected"; setup_remote; cd "$ROOT/me"; git reset -q --hard HEAD~1; run git push origin main
step "21-gc-defaults"; run 'git config --get gc.reflogExpire || echo "(unset: default 90 days)"'; run 'git config --get gc.reflogExpireUnreachable || echo "(unset: default 30 days)"'
