After installing Git, the next important step is to configure your identity and some basic Git settings. Git configuration allows you to define information such as your name and email address that Git can associate with your commits.
Git configuration means setting options that control how Git behaves and how it identifies you when creating commits.
The main configuration command is:
git config
Git uses configuration information for different operations. A common requirement is configuring your name and email address.
These values are commonly stored with commit metadata.
You can configure the name that Git associates with your commits.
git config --global user.name "Your Name"
Replace Your Name with the name you want Git to use.
You can configure an email address for your Git commits using:
git config --global user.email "you@example.com"
Use an email address appropriate for your development workflow.
The --global option applies the configuration to your user account on the computer.
git config --global user.name "Your Name"
This setting can be used by repositories for that user unless a more specific repository configuration overrides it.
You can check the configured user name with:
git config user.name
If a value is configured, Git displays the current effective value for that setting.
To check the configured email address, use:
git config user.email
You can view available Git configuration values using:
git config --list
This displays configuration settings that Git can access from the applicable configuration levels.
Global configuration applies to repositories for your user account, unless a more specific configuration overrides a value.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
A repository can have its own local configuration. To set a value for only the current repository, omit --global.
git config user.name "Project Name"
git config user.email "project@example.com"
Local configuration takes precedence over the corresponding global configuration for that repository.
Git also supports system-level configuration. The --system option applies configuration for the system installation and may require administrator privileges.
git config --system
Most beginners will normally work with global and local configuration.
Git commonly works with three configuration levels:
More specific configuration can override a value from a broader level.
Git can show where configuration values are coming from. One useful command is:
git config --list --show-origin
This can help when you need to understand why a particular value is being used.
You can change the global Git user name at any time.
git config --global user.name "New Name"
The new value will be used for future commits where the global value applies.
You can also change the configured global email address.
git config --global user.email "new@example.com"
The changed configuration affects future commits.
Git allows you to configure the default initial branch name used when creating a new repository.
git config --global init.defaultBranch main
This means newly initialized repositories can start with main as their initial branch name.
You can inspect the configured default branch setting using:
git config --global init.defaultBranch
If it has been configured, Git will display its value.
Git configuration can be opened in a text editor using:
git config --global --edit
This opens the global configuration file using Git's configured editor.
A configuration value can be removed using the --unset option.
git config --global --unset user.name
After removing a value, Git may use another applicable configuration level if one exists.
Git configuration can also be used to create command aliases. For example:
git config --global alias.st status
After this configuration, you can use:
git st
as an alias for git status.
Another common example is creating a short alias for the log command.
git config --global alias.lg "log --oneline"
Then you can use:
git lg
Git stores configuration values in configuration files at different levels.
For example, global user configuration is commonly stored in the user's Git configuration file.
You normally do not need to edit these files manually because
git config provides a safer command-line interface.
Git user name and email are used as commit metadata. They are separate from the authentication method you use to connect to GitHub.
Configuring your Git email does not by itself sign you into GitHub.
git config --global user.name "Rahul Kumar"
git config --global user.email "rahul@example.com"
git config --global init.defaultBranch main
After setting these values, you can verify them:
git config --global --list
Sometimes a developer may want different Git identity information for a particular repository.
Inside that repository, use local configuration:
git config user.name "Project Name"
git config user.email "project@example.com"
This local configuration can override the global value for that repository.
To check the effective value used for a setting, you can run:
git config user.name
git config user.email
These commands are useful when troubleshooting configuration issues.
A beginner can start with a simple configuration:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
Then verify:
git config --global --list
git config --global user.name
git config --global user.email
Git configuration allows you to customize Git and provide identity information for your commits. The most important beginner settings are your user name and email address.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
In the next lesson, we will learn about Git Bash and basic terminal commands used while working with Git.
Question: Which Git command is used to configure the user name globally?