Git Config Command

Git Config Command

The git config command is a convenience function that is used to set Git configuration values on a global or local project level. These configuration levels correspond to .gitconfig text files. Executing git config will modify a configuration text file. We’ll be covering common configuration settings like email, username, and editor. We’ll discuss Git aliases, which allow you to create shortcuts for frequently used Git operations. Becoming familiar with git config and the various Git configuration settings will help you create a powerful, customized Git workflow.

The most basic use case for git config is to invoke it with a configuration name, which will display the set value at that name. Configuration names are dot delimited strings composed of a ‘section’ and a ‘key’ based on their hierarchy. For example: user.email

git config user.email

In this example, email is a child property of the user configuration block. This will return the configured email address, if any, that Git will associate with locally created commits.

git config levels and files

Before we further discuss git config usage, let’s take a moment to cover configuration levels. The git config command can accept arguments to specify which configuration level to operate on. The following configuration levels are available:

  1. local: By default, git config will write to a local level if no configuration option is passed. Local level configuration is applied to the context repository git config gets invoked in. Local configuration values are stored in a file that can be found in the repo’s .git directory: .git/config
  2. global: Global level configuration is user-specific, meaning it is applied to an operating system user. Global configuration values are stored in a file that is located in a user’s home directory. ~ /.gitconfig on unix systems and C:\Users\\.gitconfig on windows
  3. system:  System-level configuration is applied across an entire machine. This covers all users on an operating system and all repos. The system level configuration file lives in a gitconfig file off the system root path. $(prefix)/etc/gitconfig on unix systems. On windows this file can be found at C:\Documents and Settings\All Users\Application Data\Git\config on Windows XP, and in C:\ProgramData\Git\config on Windows Vista and newer.

Thus the order of priority for configuration levels is: local, global, system. This means when looking for a configuration value, Git will start at the local level and bubble up to the system level.

Writing a value

Expanding on what we already know about git config, let’s look at an example in which we write a value:

git config –global user.email “your_email@example.com”

This example writes the value your_email@example.com to the configuration name user.email. It uses the –global flag so this value is set for the current operating system user.

To declare that identity for all repositories, use git config –global

This will store the setting in your user’s .gitconfig file: e.g. $HOME/.gitconfig or for Windows, %USERPROFILE%\.gitconfig.

git config –global user.name “Your Name”

git config –global user.email mail@example.com

Once done, you can confirm that the information is set by running:

git config –list

user.name=Your Name

user.email=youremail@yourdomain.com

The command saves the values in the global configuration file, ~/.gitconfig:

cat ~/.gitconfig:

[user]

name = Your Name

email = youremail@yourdomain.com

The repository-specific setting are kept in the .git/config file under the root directory of the repository

Remove a global identity

git config –global –remove-section user.name

git config –global –remove-section user.email

For more Git Related post click here.

  1. Git init Command
  2. Git add Command
  3. Git Commit Command
  4. Git Clone Command
  5. Git Config Command
  6. Git Alias Command
  7. Git Checkout Command
  8. Git Pull Command
  9. Git Push Command
  10. Git Fetch Command
  11. Git Status Command
  12. Git Revert Command
  13. Git Remove (rm) Command
  14. Git LOG Command
  15. Git Cheat Sheet

Happy Learning !!