How do you deal with multiple git identities?

Im working fulltime + freelancing + working on my own projects and all of them are on different gitlab emails. It was very annoying to keep remembering to set my git email to a proper one each time I switch to work on another project.

Right now I came up with something easier:

I started using just 1 gitlab profile (personal one) and added my both company e-mails as secondary emails to my personal gitlab profile.

This way I can keep my git identity the same (personal e-mail address) and if I push to company1 repo or company2 repo the commit author e-mail addres is shown email@company1.com and email@company2.com as these emails are given access to private repos and they are added to my personal gitlab account.

Just wondering if these companies will see my commits to other repos by viewing my personal gitlab profile or no? Or if there is an easier way to handle multiple git identities without having to switch between them each time I open another project and want to push some commits.

  • 2
    Look up .gitconfig / ssh config.

    It takes a bit of time to set up, but pays it back hundredfold.
  • 0
    @IntrusionCM So I will have to set it up only once and then I can just pull/commit without having to worry of e-mails getting mixed?
  • 0
    There are three levels in gitconfig: local, global, and system. I don't know if gitlab can mess with them somehow.
  • 3
    @topsecret230 Yes

    You might find easier explanations via Google, but the gist is:


    You can include for a specific directory glob a specific git configuration. If I remember correctly, glob can be recursive, so you could match

    Where ** means any subfolder.

    The git configuration would consist of the username and mail.

    SSH config for the necessary authentication setup, so that the git configured identities match a specific SSH key.
  • 3
    When I had to do that, I unset the global user details so that when I forgot to explicitly specify an identity for a repo the first commit failed and reminded me to do it.

    I also had multiple github accounts so I created an SSH host for work.
  • 1
    The whole system was really clunky and shit, and it was a web company too, I constantly felt like I should've set up a dev container instead. If your company is similar, this would be my advice.
  • 2
    I have two entirely separate accounts on GitHub, one for work and one for personal use. At first I messed around with aliases to swap my username and email as needed, but then I found an easier way. If your folder organization isn't a complete mess, you can use conditional includes based on the repo path.

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

    Then I just have my work username, email, and gpg key configured in that .work.gitconfig file. Any time I perform git operations on any repo under ~/work, it includes that other config file, overriding my personal credentials with my work credentials.

    More details (and probably a better explanation): https://medium.com/@mrjink/...
Add Comment