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:
- 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
- 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
- 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 “email@example.com”
This example writes the value firstname.lastname@example.org 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 email@example.com
Once done, you can confirm that the information is set by running:
git config –list
The command saves the values in the global configuration file, ~/.gitconfig:
name = Your Name
email = firstname.lastname@example.org
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.
- Git init Command
- Git add Command
- Git Commit Command
- Git Clone Command
- Git Config Command
- Git Alias Command
- Git Checkout Command
- Git Pull Command
- Git Push Command
- Git Fetch Command
- Git Status Command
- Git Revert Command
- Git Remove (rm) Command
- Git LOG Command
- Git Cheat Sheet
Happy Learning !!