Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aont/ad3076037d9727a432f8b065524a2573 to your computer and use it in GitHub Desktop.

Select an option

Save aont/ad3076037d9727a432f8b065524a2573 to your computer and use it in GitHub Desktop.

Automate Your Git User Setup with a Post-Checkout Hook

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.

What This Hook Does

  • Runs after every git checkout.
  • If the repository already has a local user.name and user.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.

One-Time Setup

  1. Create a directory for your hooks

    mkdir -p ~/.config/git/hooks
  2. Save the script as post-checkout

    • Path: ~/.config/git/hooks/post-checkout
    • Contents: (see script below)
  3. Make it executable

    chmod +x ~/.config/git/hooks/post-checkout
  4. 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.

How It Works (Briefly)

  • 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.
  • fzf provides an interactive selection UI.
  • The chosen name and email are written to the repo’s local config with git config --local.

Tips

  • 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!

#!/bin/bash
name="$(git config --local --get user.name)"
email="$(git config --local --get user.email)"
if [[ ! ( -z "$name" || -z "$email" ) ]]; then
exit
fi
selected=$(git shortlog -sne "$2" \
| sed -e 's/^[[:space:]]\+//g; s/[[:space:]]\+/ /g' \
| fzf --prompt "Git account>" )
# --- Selection check ---
if [[ -z "$selected" ]]; then
echo "Canceled."
exit 1
fi
# --- Extract name and email ---
# Use sed to extract "name" and "email"
name="$(echo "$selected" | sed -e 's/^[0-9]\+[[:space:]]\+//g; s/[[:space:]]*<.*>$//g;')"
email="$(echo "$selected" | sed -e 's/^.*<//g; s/>$//g')"
# --- Set git config ---
git config --local user.name "$name"
git config --local user.email "$email"
echo "\
✅ Configuration complete:
user.name = $name
user.email = $email"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment