Skip to main content

How to Configure Git Users

·3 mins

Introduction #

A Git user consists of a name and email address, which are used to identify the author of each commit. These details can be configured at any time, but changes will only apply to future commits.

GitHub uses the commit information to associate the commit author with a GitHub account, in particular the email address.

Setting up a single Git user is straightforward and requires a one-time configuration, while managing multiple Git users involves a bit more effort.

Configuration #

Git configuration can be managed using the git config tool, which supports various configuration scopes to accommodate different use cases.

System #

System-wide configuration is applied to all users and all their repositories. This configuration is stored in the file /etc/gitconfig.

Caution

It is not recommended to configure user information within this scope.

Global #

Global configuration is applied to the current system user and all their repositories. This configuration is stored in the file ~/.gitconfig or ~/.config/git/config. Global configuration will override values defined in the previous scope, System.

To set the user name in the global configuration file

git config --global user.name "Global User"

To set the user email address in the global configuration file

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

To list all the values in the global configuration file

git config --global --list

Local #

Local configuration is applied to a single repository and is stored in the Git repository at .git/config. Local configuration will override values defined in the previous scopes, System and Global.

Note

The following commands can only be used inside a Git repository.

To set the user name for a specific repository

git config user.name "Local User"

To set the user email address for a specific repository

git config user.email "[email protected]"

To list all the values for a specific repository

git config --local --list

Summary #

Setting up a single Git user is a simple process. By using the commands outlined in the Global section, you can configure a Git user for the current system user and be done with it.

Managing multiple Git users can extend on this approach by setting the most frequently used user at the Global scope and defining alternative users at the Local scope for individual repositories.

Advanced #

If you find yourself frequently working on multiple repositories as different Git users, the following script may simplify the experience.

Create a PATH accessible executable file with the contents below, alter the DEFAULT_ values as desired, and use it inside a Git repository whenever configuration is required.

#!/bin/bash
#
# Configure a Git user within the repository configuration file.
set -e

# default variable for user name
readonly DEFAULT_USER_NAME='Local User'

# default variable for user email address
readonly DEFAULT_USER_EMAIL='[email protected]'

git_config() {
  local name="${1}"
  local value="${2}"

  # allow value override via standard input
  read -e -p "${name} [${value}]: " input

  # set the name value in the repository config file
  git config "${name}" "${input:-$value}"

  # print the name value from the repository config file
  echo "${name}=$(git config --local ${name})"
}

git_config 'user.name' "${DEFAULT_USER_NAME}"
git_config 'user.email' "${DEFAULT_USER_EMAIL}"