Skip to content

Instantly share code, notes, and snippets.

@lelandbatey
Created January 9, 2025 22:27
Show Gist options
  • Save lelandbatey/b42b527bfa41b761d0aec2871d32748a to your computer and use it in GitHub Desktop.
Save lelandbatey/b42b527bfa41b761d0aec2871d32748a to your computer and use it in GitHub Desktop.
Scold for using Git Checkout
# Paste this snippet into your .bashrc to help you learn `git switch`.
#
# Turn on the extdebug mode so that our `scold_git_checkout` can prevent
# us from running git-checkout by exiting 1; forcing us to re-learn our
# muscle memory.
# If we *really* need to use git checkout for something, we can do that
# by using the full path to the git executable e.g.:
# /usr/bin/git checkout # isn't caught by our scolding
shopt -s extdebug
scold_git_checkout() {
if [ "$1" != "git" ]; then
return 0
fi
if [ "$2" != "checkout" ]; then
return 0
fi
cat >&2 <<EOF
////// DON'T use git checkout! //////
The 'git checkout' command of git has been replaced with two other
commands: 'git switch' and 'git restore'. You used:
$@
EOF
if [ "$3" == "-b" ]; then
PASTESAFE="$(printf "%q " "${@:4}")"
cat >&2 <<EOF
You should use the following:
git switch -c $PASTESAFE
EOF
elif [ "$3" == "--" ]; then
PASTESAFE="$(printf "%q " "${@:4}")"
cat >&2 <<EOF
You should use the following:
git restore $PASTESAFE
EOF
else
PASTESAFE="$(printf "%q " "${@:3}")"
cat >&2 <<EOF
You can try the following:
git switch $PASTESAFE
EOF
fi
return 1
}
trap 'scold_git_checkout $BASH_COMMAND' DEBUG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment