When you work across multiple repositories—or switch between personal and work projects—it’s easy to forget which Git identity (name and email) a repo should use. This simple Git hook helps by prompting you to pick a past author identity whenever you check out a branch, then automatically sets user.name and user.email for that repository.
- Runs after every
git checkout. - If the repository already has a local
user.nameanduser.email, it does nothing. - Otherwise, it scans the repo’s history for author names/emails, shows them in an interactive picker (
fzf), and lets you choose. - It then writes the chosen identity to the repo’s local config.
Requirement: You’ll need fzf installed for the interactive picker.
-
Create a directory for your hooks
mkdir -p ~/.config/git/hooks -
Save the script as
post-checkout- Path:
~/.config/git/hooks/post-checkout - Contents: (see script below)
- Path:
-
Make it executable
chmod +x ~/.config/git/hooks/post-checkout -
Tell Git to use your hooks directory
git config --global core.hooksPath ~/.config/git/hooks
Now this hook will run for any repository using your global Git config.
git config --local --get …checks if the repo already defines a user—if yes, the script exits quietly.git log --format='%an %ae'lists author names/emails from commit history.- The pipeline (
sort | uniq -c | sed | sort -n -k 1 -r) deduplicates, counts, and ranks identities by frequency so your most-used identities appear first. fzfprovides an interactive selection UI.- The chosen
nameandemailare written to the repo’s local config withgit config --local.
-
If you frequently switch identities, consider adding more hooks (e.g.,
post-commit) with similar logic. -
You can prefill defaults by setting global values:
git config --global user.name "Your Name" git config --global user.email "you@example.com"
The hook only overrides when a repo has no local setting.
That’s it—enjoy faster, safer identity switching across your Git projects!