If you work with different users in personal and work projects, it is interesting to set up your username and email at GIT locally, by project, so that your commits are correctly tracked on Github, Gitlab and Bitbucket.

By default the /<users>/.gitconfig file is used to store your global user and email settings to GIT.

Other Git resources:

.gitconfig file Contents#

This is an example of how a .gitconfig configuration file is composed:

[user]
	name = Ademir Mazer Junior
	email = email [at] mazer.dev
[pull]
	rebase = false
[commit]
	gpgSign = false
[gui]
	pruneduringfetch = true
[smartgit "submodule"]
	fetchalways = true
	update = true
	initializenew = true
[push]
	recurseSubmodules = check

As you can check, the tag [user] is used for all GIT clients to find their username and email to configure the push and committs commands.

Configuring Username and Email by Project at Git#

GIT offers three possibilities to define your username and email. Settings will be considered in the following order, where 1 > will replace 2 > that will replace 3.

  1. project - Data is stored directly on the corresponding project (repository) in git/config file.
  2. global - for all current user repositories. The configuration file can be found in your home directory at ~/.gitconfig .
  3. system - This is a configuration for the entire system and is available to all users and all repositories. You will find it in Linux systems directories, in general in /etc/gitconfig.

In most cases you will define a global configuration for the user and then define the specific settings of each project.

Let’s check the three configuration levels, from the most wide (item 3 above) to the most specific, item 1 - by project:

Configuring Username and Email for the entire system in Git#

This is not such a common indication in tutorials and courses, but you can configure for all users of an operating system a username and email to GIT:

git config --system user.name "System username"
git config --system user.email "[email protected]"

This is therefore the global configuration for all user of your operating system.

Configuring Username and Email globally on Git#

Remembering the most widespread configuration commands when installing Git:

git config --global user.name "My global name"
git config --global user.email "[email protected]"

This is the global configuration for your operating system user.

Configuring Username and Email on Git for a specific project#

Finally, this option overlaps the previous settings for each project in which it is used. Similarly to the other options, you will use the commands git config, inside of the directory of your project, but this time without using the global or system option:

# Remember to navigate to the project root
cd my-project
git config user.name "My name in this project"
git config user.email "[email protected]"

You can also use the --local option to “guarantee” the local configuration.

Checking the username and email settings#

To check how the settings are saved, use the git config command with the option --get:

git config --get user.name
git config --get user.email

The above commands will show the local settings to check globally or to the system, add the --global or --system options.

References:#