A Detailed Guide to Git Configuration: From User Identity to Custom Settings
Configuring Git is the first step in using Git, and proper configuration can make your work more efficient and professional. This lesson will provide a detailed explanation of all aspects of Git configuration.
1. Overview of Git Configuration
Git configuration (git config) is used to set how Git operates and to specify user information. Configuration information is stored in configuration files, and Git reads these configurations in order of priority.
(1) Why is configuration necessary?
Git needs the following information:
- User Identity: Author information (name, email address) is recorded with each submission.
- Editor Selection: The text editor used when writing commit messages
- Behavior Preferences: Default branch names, line break handling, colored output, etc.
- Productivity Tools: Command aliases, auto-completion, etc.
(2) Basic Syntax of Configuration Commands
git config [<Level>] <Configuration Options> <value>
Level Parameters:
--system: System-wide (all users)--global: Global level (current user)--local: Local level (current repository)- If no level is specified, the default is
--local
2. User Identity Configuration
User identity is the most important Git configuration; this information is recorded with every commit.
(1) Why is it necessary to configure identity?
Imagine a world without identification:
Submit1:Added a login feature(Who is the author??)
Submit2:Fixedbug(Who fixed it??)
Submit3:Performance has been optimized(Is there a way to contact the author??)
The Role of Identity Information:
- Commit History: Each commit records the author and submitter information
- Team Collaboration: Team members can identify who made the changes
- Accountability: The ability to quickly identify the person responsible when a problem arises
- Account Linking: GitHub/GitLab will link your account based on your email address
(2) Configure the username and email address
# Configure Global Username(We recommend using your real name or a nickname.)
git config --global user.name "Zhang San"
# Configure a Global Email Account(Recommended for useGitHub/GitLabAccount Email)
git config --global user.email "zhangsan@example.com"
▶ Example: Configuring and Verifying Identity Information
# Steps1:Configure Username
git config --global user.name "Zhang San"
# Steps2:Set Up Email
git config --global user.email "zhangsan@example.com"
# Steps3:Verify Configuration
git config user.name
# Output:Zhang San
git config user.email
# Output:zhangsan@example.com
# Steps4:View the contents of the configuration file
cat ~/.gitconfig
# Output:
# [user]
# name = Zhang San
# email = zhangsan@example.com
3. Configuration Levels and Priorities
Git configuration is divided into three levels, in descending order of priority: local > global > system.
(1) Detailed Explanation of Configuration Levels
graph TB
A[Local Configuration<br/>--local<br/>Priority:Highest] --> B[Global Configuration<br/>--global<br/>Priority:Intermediate]
B --> C[System Configuration<br/>--system<br/>Priority:Lowest]
style A fill:#e1f5ff
style B fill:#fff4e1
style C fill:#f0f0f0
Configuration Level Description Table:
| Level | Parameter | File Location | Scope | Use Case |
|---|---|---|---|---|
| Local Configuration | --local |
.git/config |
Current Repository | Project-Specific Configuration |
| Global Settings | --global |
~/.gitconfig |
All repositories for the current user | Personal preferences |
| System Configuration | --system |
/etc/gitconfig |
All system users | System default configuration |
(2) Configuration File Location
Windows:
System Configuration:C:\ProgramData\Git\config
Global Configuration:C:\Users\Username\.gitconfig
Local Configuration:Project Directory\.git\config
Linux/Mac systems:
System Configuration:/etc/gitconfig
Global Configuration:~/.gitconfig
Local Configuration:Project Directory/.git/config
(3) Priority Rules
When the same configuration option appears at multiple levels, Git uses the value with the highest priority:
Local Configuration(Highest)→ Global Configuration(Intermediate)→ System Configuration(Lowest)
▶ Example: Configuration at Different Levels
# Scene:Global Use"Zhang San",But a certain project requires the use of"Li Si"
# Steps1:Configure Global Identity(Applicable to all projects)
git config --global user.name "Zhang San"
git config --global user.email "zhangsan@example.com"
# Steps2:Go to a specific project directory
cd /path/to/special-project
# Steps3:Configure Local Identity(Current project only)
git config --local user.name "Li Si"
git config --local user.email "lisi@company.com"
# Steps4:Verify the identity used for the current project
git config user.name
# Output:Li Si(Local settings override global settings.)
# Steps5:Verify Global Identity(In other projects)
cd /path/to/other-project
git config user.name
# Output:Zhang San(Using Global Settings)
4. Common Configuration Options
In addition to user accounts, there are many useful settings that can help you work more efficiently.
(1) Editor Configuration
Git launches a text editor whenever text input is required (such as when writing a commit message).
# Set the default editor toVS Code(Recommendations)
git config --global core.editor "code --wait"
# Set the default editor toVim
git config --global core.editor "vim"
# Set the default editor toNano
git config --global core.editor "nano"
# Windows:Set the default editor toNotepad++
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
(2) Automatic Line Break Configuration
Different operating systems use different line break characters:
- Windows:CRLF(
\r\n) - Linux/Mac: LF (
\n)
# WindowsRecommended User Configurations
# Convert upon submission toLF,Convert to upon detectionCRLF
git config --global core.autocrlf true
# Linux/MacRecommended User Configurations
# Convert upon submission toLF,Do not convert when detected
git config --global core.autocrlf input
# Disable Auto-Conversion(Not recommended)
git config --global core.autocrlf false
(3) Command Alias Configuration
Aliases allow you to assign short names to frequently used commands, greatly improving efficiency.
# Set Aliases for Common Commands
git config --global alias.st status # git st = git status
git config --global alias.co checkout # git co = git checkout
git config --global alias.br branch # git br = git branch
git config --global alias.ci commit # git ci = git commit
git config --global alias.unstage 'reset HEAD --' # git unstage
# Set an alias with options
git config --global alias.last 'log -1 HEAD' # git last = View the last commit
git config --global alias.lg "log --graph --oneline --decorate --all" # Formatted Logs
▶ Example: Practical Configuration Combinations
# Steps1:Set the default branch name tomain(rather thanmaster)
git config --global init.defaultBranch main
# Enable Color Output (make git output easier to read)
git config --global color.ui auto
# Steps3:Set to use when pullingmerge(rather thanrebase)
git config --global pull.rebase false
# Steps4:Configure push settings to push only to the current branch
git config --global push.default simple
# Steps5:Turn on AutoCorrect(Automatically attempt to correct a mistyped command)
git config --global help.autocorrect 1
# Steps6:Verify all configurations
git config --global --list | grep -E "(init|color|pull|push|help)"
# Output:
# init.defaultbranch=main
# color.ui=auto
# pull.rebase=false
# push.default=simple
# help.autocorrect=1
5. View and Manage Configurations
Knowing how to view and manage configurations is a fundamental skill for using Git.
(1) View All Settings
# View All Configurations(Results of the Merger of the Three Levels)
git config --list
# View Global Settings
git config --global --list
# View Local Configuration
git config --local --list
# View System Configuration
git config --system --list
(2) View a single configuration item
# View Username
git config user.name
# Check Email
git config user.email
# View Editor
git config core.editor
# View the default branch name
git config init.defaultBranch
(3) Modify configuration settings
# Change Username(Overwrite directly)
git config --global user.name "New Name"
# Change Email Address
git config --global user.email "new.email@example.com"
(4) Delete a configuration item
# Delete Global Configuration Items
git config --global --unset user.name
# Delete Local Configuration Entries
git config --local --unset user.name
# Delete Alias
git config --global --unset alias.st
▶ Example: View configuration information
# Steps1:View All Configurations
git config --list
# Output Example:
# user.name=Zhang San
# user.email=zhangsan@example.com
# core.editor=code --wait
# init.defaultbranch=main
# color.ui=auto
# alias.st=status
# alias.co=checkout
# alias.br=branch
# alias.ci=commit
# Steps2:View a Single Configuration
git config user.name
# Output:Zhang San
# Steps3:View Configuration Source(Show which file the configuration comes from)
git config --show-origin user.name
# Output:file:C:/Users/Zhang San/.gitconfig Zhang San
# Steps4:View all configurations and their sources
git config --list --show-origin
# Output Example:
# file:C:/ProgramData/Git/config core.symlinks=false
# file:C:/Users/Zhang San/.gitconfig user.name=Zhang San
# file:.git/config core.repositoryformatversion=0
❓ FAQ
git config --local --list to check the local configuration, or use git config --show-origin user.name to view the configuration source.git config --global --unset 配置项名称—for example, git config --global --unset user.name—to delete the global username configuration.git config command to avoid syntax errors.📖 Summary
- Git configuration is used to set up your user identity, editor, behavior preferences, and more; it is the first step in using Git.
- Configurations are organized into three levels: Local (highest priority) → Global → System (lowest priority)
- You must configure your username and email address; this information will be recorded with every submission.
- Common configurations include: editor, line ending handling, command aliases, default branch names, etc.
- Use
git config --listto view all configurations, andgit config <key>to view a single configuration - Configuration file location: Globally at
~/.gitconfig, locally at.git/config
📝 Exercises
-
Basic Exercise (Difficulty: ⭐): Configure your global Git credentials (username and email address), and verify that the configuration was successful.
-
Advanced Exercise (Difficulty: ⭐⭐): Set up at least three useful configuration options (such as default branch names, color output, and command aliases), and explain the purpose of each option.
-
Challenge (Difficulty: ⭐⭐⭐): Create a test repository, configure a local identity in that repository (different from the global identity), verify that the local configuration overrides the global configuration, and explain the priority rules.



