Thursday, 5 June 2014

Multiple git identities

You may wish to keep your work and personal git identities separate but use them both on the same machine.

One way to do this is to use aliases in your ssh and git configs.

Generate a second key and upload it to github.

In ~/.ssh/config

# Opensource github user
Host githubAlias
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_opensource

In the non-default case, Melati in this example, we now use the alias within the project configuration and override user.name and user.email in .git/config:

[core]
 repositoryformatversion = 0
 filemode = true
 bare = false
 logallrefupdates = true

[user]
 email = timp@paneris.org
 name = Tim Pizey

# NOTE use of remote alias defined in ~/.ssh/config
[remote "origin"]
 fetch = +refs/heads/*:refs/remotes/origin/*
 url = git@githubAlias:Melati/Melati.git
[branch "master"]
 remote = origin
 merge = refs/heads/master

Swapping

At some point you will forget or slip up in some other way.

#!/bin/sh
 
git filter-branch --env-filter '
 
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"
 
if [ "$GIT_COMMITTER_EMAIL" = "timp@paneris.org" ]
then
    cn="Tim Pizey"
    cm="timp21337@paneris.org"
fi
if [ "$GIT_AUTHOR_EMAIL" = "timp@paneris.org" ]
then
    an="Tim Pizey"
    am="timp21337@paneris.org"
fi
 
export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'
Then git push -f origin master

No comments:

Post a Comment