Skip to content

Instantly share code, notes, and snippets.

@Jibbarth
Last active May 3, 2020 15:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Jibbarth/2bf3a3a752dc8ac178fc6c1b6348da48 to your computer and use it in GitHub Desktop.
Automatically managing personal and work git email

In order to differentiate your professional and personal email address when working on git repositories, here is how I proceeded.

1. Create a global git hooks folder

Create a folder where you will drop the hooks that should apply to each repository. And set the core.hooksPath on this folder

git config --global core.hooksPath /path/to/my/centralized/hooks

2. Create a post-checkout hook

The hook post-checkout is triggered when you change branch, and also on the git clone. So it'll be executed for existing repo, and those you will clone later.

See post-checkout file below to see what I put in.

Don't forget to add execution permission on your new hook :

chmod +x post-checkout

⚠ The core.hooksPath support is new in Git version 2.9.

#!/bin/bash
set -e
companyEmail='name@my-awesome-company.com'
personalEmail='name@personal.com'
companyName='my-awesome-company'
repoUrl=`git config --get remote.origin.url`
currentEmail=`git config user.email`
if [[ $repoUrl == *$companyName* ]] && [ "$currentEmail" != "$companyEmail" ]
then
git config user.email $companyEmail
printf "🏢 It's a company repository. \n\nThe git user.email config has been setted to $companyEmail 🎉 \n"
fi
# Reset classic local git hooks
git config core.hooksPath ".git/hooks"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment