How have different git accounts in our local environment

Sebastián Vidal Aedo
2 min readJan 11, 2022
split-movie
Split (2016)

Sometimes we need to have accounts in Github and Gitlab, or even in the same Git environment, but with different emails (Maybe for work in more than a Company where is mandatory use the company email/account). This is normal as a developer, the problem appear when we need have instances in our local environment under differents git accounts, because the push can leave wrong info, an example, imagine you have an account in gitlab and other in github under different emails and names, something like this:

github => Jack Frost <jack@mail.com>
gitlab => Jack Frost Winter <jfrost@company.com>

Here we have two accounts for the same developer, is probably in our dev machine we have a default account, and if we “git push” some repo, exist the possibility we leaving a “commit” in the repo history with different owner (and even your own “commit” can have different name or email).

Because this happen, git give us the posibility to config especific info in three different levels:

System: config saved in /etc/gitconfig
Global: config saved in ~/.gitconfig
Project: config saved in .git/config in the specific project

As you can assum, the System config is default, then the usual Global config and the most specific is Projecto config. Using the last one, you can config differents accounts for any project in you local machine.

How you can set it?

System
$ git config — system user.name “Jack Frost”
Global
$ git config — global user.name “Jack Frost”
Project
$ git config user.name “Jack Frost Winter”

In this last example, when we see all properties in the config file of the project project/.git/config, you will see something like this

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
hooksPath = .husky
[remote “origin”]
url = token@repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch “master”]
remote = origin
merge = refs/heads/master
[user]
name = Jack Frost Winter
email = jfrost@company.com

The section [user] will be use it in the commit info of this project

Extra

If you wanna see the full list of options you can run

$ git config -l

--

--