Skip to content

Instantly share code, notes, and snippets.

@particleflux
Created January 25, 2021 21:31
Show Gist options
  • Save particleflux/27759f807fad6b7e1e7af28dfb695b1e to your computer and use it in GitHub Desktop.
Save particleflux/27759f807fad6b7e1e7af28dfb695b1e to your computer and use it in GitHub Desktop.
Check git user config on first local commit
#!/usr/bin/env bash
verified=$(git config --type=bool hooks.userverified)
# grab input
exec < /dev/tty
exec 1>&2
if [[ "$verified" != "true" ]]; then
echo "Checking committer info..."
echo "Current author information:"
echo "$(git config user.name) <$(git config user.email)>"
while read -p "Is the author set correct? (y/n) " yn; do
case $yn in
[Yy] )
echo "First local commit correct, disabled check"
git config hooks.userverified true
break
;;
[Nn] )
echo "Please update author info!";
exit 1
;;
* )
echo "Please answer y (yes) or n (no):" && continue;
esac
done
fi
@particleflux
Copy link
Author

particleflux commented Jan 25, 2021

chmod +x and store in your global git templates dir :)

In case you don't have one, create a directory at any location you prefer (mine is ~/.git-templates), and set is as template dir:

mkdir -p ~/.git-templates
#...
# move this script there manually
#...
chmod +x ~/.git-templates/prepare-commit-msg
git config --global init.templatedir ~/.git-templates

This will auto-add the hook to all newly created/cloned repos, and on the first commit in each of them, ask you to verify your user information.

No more accidental commits with wrong name/email!

@DBX12
Copy link

DBX12 commented Jan 26, 2021

Small note: You should probably run git config --global init.templatedir ~/.git-templates to configure the template dir globally.

@particleflux
Copy link
Author

Indeed, fixed above, thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment