#!/usr/bin/env bash
# Exercise 15.2: change a file, see the change with git diff, then throw it away with git restore.
set -e
export GIT_AUTHOR_NAME="Sara Khan" GIT_AUTHOR_EMAIL="sara@example.com"
export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME" GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
export GIT_CONFIG_GLOBAL=/dev/null  # ignore your own Git settings while the script runs
run() { local s="\$"; for a in "$@"; do if [[ $a == *" "* ]]; then s+=" \"$a\""; else s+=" $a"; fi; done; echo "$s"; "$@"; }
cd "$(mktemp -d)"
git init -q -b main
printf '<h1>Sara Khan</h1>\n<p>Beginner web developer</p>\n' > index.html
git add . && git commit -q -m "Add profile page"

echo "# A change we will regret:"
printf '<h1>Sara Khan</h1>\n<p>Oops, I deleted my tagline</p>\n' > index.html
run git diff --no-color
run git restore index.html
run git status --short
run cat index.html
echo "# Clean again: the file is back to the last commit."
