Can I specify multiple users for myself in .gitconfig?

In my ~/.gitconfig, I list my personal email address under [user], since that's what I want to use for Github repos.

But, I've recently started using git for work, too. My company's git repo allows me to commit, but when it sends out announcements of new changesets, it says they are from Anonymous because it doesn't recognize the email address in my .gitconfig - at least, that's my theory.

Is it possible to specify multiple [user] definitions in .gitconfig? Or is there some other way to override the default .gitconfig for a certain directory? In my case, I check out all work code in ~/worksrc/ - is there a way to specify a .gitconfig for only that directory (and its subdirectories)?


Solution 1:

You can configure an individual repo to use a specific user / email address which overrides the global configuration. From the root of the repo, run

git config user.name "Your Name Here"
git config user.email [email protected]

whereas the default user / email is configured in your ~/.gitconfig

git config --global user.name "Your Name Here"
git config --global user.email [email protected]

Solution 2:

Since git 2.13, it is possible to solve this using newly introduced Conditional includes.

An example:

Global config ~/.gitconfig

[user]
    name = John Doe
    email = [email protected]

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig

Work specific config ~/work/.gitconfig

[user]
    email = [email protected]

Remember that [includeIf...] should follows default [user] at the top.

Solution 3:

Or you can add following information in your local .git/config file

[user]  
    name = Your Name
    email = [email protected]

Solution 4:

One command github accounts switch

This solution takes the form of a single git alias. Once executed, the current project user will be attached to another account

Generate ssh keys

ssh-keygen -t rsa -C "[email protected]" -f '/Users/arnaudrinquin/.ssh/id_rsa'

[...]

ssh-keygen -t rsa -C "[email protected]" -f '/Users/arnaudrinquin/.ssh/id_rsa_pro'

Link them to your GitHub / Bitbucket accounts

  1. copy default public key pbcopy < ~/.ssh/id_rsa.pub
  2. login to your GitHub acount
  3. paste the key in the add SSH key github page
  4. copy other public key pbcopy < ~/.ssh/id_rsa_pro.pub
  5. repeat and adapt steps 2 to 4 for every other account

Step 1. Automatic ssh key switching.

We can configure ssh to send a use a specific encryption key depending on the host. The nice thing is that you can have several aliases for the same hostname.

See this example ~/.ssh/config file:

# Default GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

# Professional github alias
Host github_pro
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_pro

git remote configuration

You can now use these aliases in the git remotes by changing [email protected] by git@github_pro.

You can either change your existing projects remotes (using something like git remote set-url origin git@github_pro:foo/bar.git) or adapt them directly when cloning them.

git clone [email protected]:ArnaudRinquin/atom-zentabs.git

using alias, it become:

git clone git@github_pro:ArnaudRinquin/atom-zentabs.git

Step 2. Changing git user.email

Git config settings can be global or per project. In our case, we want a per project settings. It is very easy to change it:

git config user.email '[email protected]'

While this is easy, it takes way to long for the developers we are. We can write a very simple git alias for that.

We are going to add it to the ~/.gitconfig file.

[user]
    name = Arnaud Rinquin
    email = [email protected]

...

[alias]
    setpromail = "config user.email '[email protected]'"

Then, all we have to do is git setpromail to have our email changed for this project only.

Step 3. One command switch please?!

Wouldn’t it be nice to switch from default account to a specified one with a single parameter-less command? This is definitely possible. This command will have two steps:

  • change current project remotes to the chosen aliases
  • change current project user.email config

We already have a one command solution for the second step, but the first one is way harder. One command remote host change

Here comes the solution in the form of another git alias command to add to your ~/.gitconfig:

[alias]
  changeremotehost = !sh -c \"git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url\"

This allows changing all remotes from one host to another (the alias). See the example:

$ > git remote -v
origin  [email protected]:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin  [email protected]:ArnaudRinquin/arnaudrinquin.github.io.git (push)

$ > git changeremotehost github.com github_pro

$ > git remote -v
origin  git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin  git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (push)

Combine them all

We now just have to combine the two commands into one, this is quite easy. See how I also integrate bitbucket host switching.

[alias]
  changeremotehost = !sh -c \"git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url\"
  setpromail = "config user.email '[email protected]'"
  gopro = !sh -c \"git changeremotehost github.com github_pro && git changeremotehost bitbucket.com bitbucket_pro && git setpromail\"

Source Link -Github

Source Link -Tutorial