DEV Community

loading...
Cover image for How to setup different git emails on your computer

How to setup different git emails on your computer

myrtle profile image Mrtl ・3 min read

I needed to write this down or I will forget, like I do with most setup-type of info 🌝
This has worked really well for me and saved me a lot of time, typing, and mistakes..
so why not share!


πŸ“™ skip the faff, jump to commands

or read on!

This is for you if:

  • You've got different emails/accounts on github, gitlab, bitbucket etc.
  • You'd like your commits/contributions to come from different accounts, ie. one personal & one business.
  • Most often, you need to setup a work email/account and use that on git for all your work. But you still want to doodle on your projects in your spare time.

Global git configuration is not great in this case, switching git accounts all the time will eventually turn frustrating.

πŸ”§ we can setup git by folder

Simply remove the global git config completely, then set up the accounts as we wish into directories.
What worked for me was an easy separation of work and personal directories at low level, so any folders within that tree will come under the chosen git account.

the structure looks like this:

user/
β”œβ”€β”€ .gitconfig 
β”œβ”€β”€ work/
β”‚   └── .gitconfig
└── personal/
    └── .gitconfig
Enter fullscreen mode Exit fullscreen mode

you may, of course, choose or name them folders as you like, I am using work & personal as an example
you can have as many of them as you want

solution

βš™ (one of many, perhaps the easiest)

in your main user/home folder
undo your global config
$ git config --global --unset user.name
$ git config --global --unset user.email

create your folders
$ mkdir work
$ mkdir personal

add gitconfig files in the folders
$ touch ~/work/.gitconfig ~/personal/.gitconfig

add the git name and email to each .gitconfig in the folders
either manually or through the cli
$ nano ~/work/.gitconfig

add your lines as

[user]
        email = {example@email.com}
        name = {name}
Enter fullscreen mode Exit fullscreen mode

repeat with $ nano ~/personal/.gitconfig

finally we need to tell the global config where to look for the right details
make sure you are in your main user folder where the global gitconfig lives
$ nano ~/.gitconfig

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig
[includeIf "gitdir:~/personal/"]
    path = ~/personal/.gitconfig
Enter fullscreen mode Exit fullscreen mode

tada πŸŽ‰ ✨



optional sanity check with
$ git config --list
which should return, amongst other setup info

includeif.gitdir:~/work/.path=~/work/.gitconfig
includeif.gitdir:~/projects/.path=~/projects/.gitconfig
Enter fullscreen mode Exit fullscreen mode

⇨ best way to double check it all works is to go into your folders and try to interact with private remote repos that your accounts have access to - if you can, for example, pull a private repo, then the magic setup worked 🀩


πŸ’ cherry tip

⇨ You can connect your github account with multiple git emails, so it will recognise your contributions no matter what email you committed with.
GitHub allows you to add as many email addresses to your account as you like.




ideas

Discussion (0)

pic
Editor guide