Git config different emails for Github & BitBucket

in #git6 years ago (edited)

Hi all, today I want to share a little trick I found for git config files.

For a long time I've been facing a small but annoying problem. When using git on command line you have to set your name and email. You can either do this globally or locally.

In my case I'm creating and downloading new repos often. The end result accidentally signing a work commit with my personal email, or vise versa.

I seem to have found a good solution.

Instead of having a hard-coded config a post-checkout hook is run.

This hook is invoked when a git-checkout is run after having updated the worktree. The hook is given three parameters: the ref of the previous HEAD, the ref of the new HEAD (which may or may not have changed), and a flag indicating whether the checkout was a branch checkout (changing branches, flag=1) or a file checkout (retrieving a file from the index, flag=0). This hook cannot affect the outcome of git-checkout.

File location ~/.git-templates/hooks/post-checkout make sure to make it executable with chmod +x ~/.git-templates/hooks/post-checkout.


#!/usr/bin/env bash

# make regex matching below case insensitive
shopt -s nocasematch

# values in the services array should have a corresponding section in
# .gitconfig where the 'name' and 'email' for that service are specified
remote_url="$( git config --get --local remote.origin.url )"
services=(
    'github'
    'bitbucket'
)

set_local_user_config() {
    local service="${1}"
    local config="${2}"
    local service_config="$( git config --get ${service}.${config} )"
    local local_config="$( git config --get --local user.${config} )"

    if [[ "${local_config}" != "${service_config}" ]]; then
        git config --local "user.${config}" "${service_config}"
        echo "repo 'user.${config}' has been set to '${service_config}'"
    fi
}

# if remote_url doesn't contain the any of the values in the services
# array the user name and email will remain unset and the
# user.useConfigOnly = true setting in .gitconfig will prompt for those
# credentials and prevent commits until they are defined
for s in "${services[@]}"; do
    if [[ "${remote_url}" =~ "${s}" ]]; then
        set_local_user_config "${s}" 'name'
        set_local_user_config "${s}" 'email'
        break
    fi
done

The script searches the remote url for the service string "bitbucket" or "github" it will then reference our config file that looks something like:

[github]
    name = Philip Kirkbride
    email = [email protected]
[bitbucket]
    name = Philip Kirkbride
    email = [email protected]
[init]
    templatedir = ~/.git-templates
[user]
    useConfigOnly = true

Great solution, all credit to Grant Humphries on StackoverFlow.


Since I don't have a photo for this post here is an escalators under-construction that I saw on the way to the coffee shop this morning.

IMG_20180519_111644.jpg

Sort:  

You call them elevators instead of escalators in Canada? Did not know that.

I had the same problem, but I solved it with gpg

I usually handle my commits with: git commit -S which will sign my commit using a PGP key. This solved the problem for me because it forces me to use a passphrase on every commit. Of course, my passphrases are different for each identity/key. If I am committing to a work repo using my personal identity, my key signing will fail because I use the wrong passphrase.

I like the idea about using the hooks to add contact info automatically, but I'm wary of it.

When trying to commit with pgp signature, there is an error if no contact info exists (it can't sign the commit).

Then, I just manually add my contact info by editing the .git/config.

I just add the following to my repo .git/config

[user]
        name = Leo Przybylski
        email = [email protected]
        signingkey = <pgp key>

I know, it's lo-tech, but I like lo-tech for this kind of thing. Plus PGP signatures make it so my coworkers and everyone else on the planet can validate my commits to my identity.

Thanks for the tip. The other nice thing with PGP you get the cool little check-mark by your commit if it's on Github!

Coin Marketplace

STEEM 0.25
TRX 0.11
JST 0.033
BTC 62480.78
ETH 3045.78
USDT 1.00
SBD 3.91