Skip to content

Instantly share code, notes, and snippets.

@lbt
Created April 6, 2021 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbt/195b6232cc32f81f5722380503d98cdb to your computer and use it in GitHub Desktop.
Save lbt/195b6232cc32f81f5722380503d98cdb to your computer and use it in GitHub Desktop.
Change git identity
In your config add sections for user-$IDENTITY. Below I use -work and -home
Also add an alias to run a script when you want to change .. I chose "user" ... "become" might be fun...
$ cat ~/.gitconfig
<---------snip---------->
[user]
name = David Greaves
email = david.greaves@jolla.com
[user-home]
name = David Greaves
email = david@dgreaves.com
[user-work]
name = David Greaves
email = david.greaves@jolla.com
[alias]
user = !/home/david/.gitconfig.d/change_user.sh
<---------snip---------->
Then add this script (and make it executable):
mkdir -p ~/.gitconfig.d
cat <<EOF > ~/.gitconfig.d/change_user.sh
#!/bin/bash
src=$1
if [[ $src == "" ]]; then
# Who am I?
git config --global --get user.name
git config --global --get user.email
else
# Alter ego...
git config --global user.name "$(git config --global user-${src}.name)"
git config --global user.email "$(git config --global user-${src}.email)"
fi
EOF
chmod +x ~/.gitconfig.d/change_user.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment