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:

(2) Basic Syntax of Configuration Commands

BASH
git config [<Level>] <Configuration Options> <value>

Level Parameters:


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:

TEXT
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:

(2) Configure the username and email address

BASH
# 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

BASH
# 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
💡 Tip: We recommend using the email address associated with your GitHub/GitLab account, so that your commit history will be linked to your account and display your avatar and username.


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

100%
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:

TEXT
System Configuration:C:\ProgramData\Git\config
Global Configuration:C:\Users\Username\.gitconfig
Local Configuration:Project Directory\.git\config

Linux/Mac systems:

TEXT
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:

TEXT
Local Configuration(Highest)→ Global Configuration(Intermediate)→ System Configuration(Lowest)

▶ Example: Configuration at Different Levels

BASH
# 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)
⚠️ Note: Generally, you only need to configure this at the global level, unless a specific project requires a special email address (e.g., a company email for company projects and a personal email for personal projects).


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).

BASH
# 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:

BASH
# 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.

BASH
# 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

BASH
# 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

BASH
# 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

BASH
# 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

BASH
# 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

BASH
# 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

BASH
# 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

Q Does the email address I configure have to match my GitHub email address?
A It's not required, but we strongly recommend consistency. If you use the same username, GitHub will link the commit history to your account, display your profile picture and username, and make your contribution history clearer.
Q Why does my submission show the wrong username?
A The local configuration may be overriding the global configuration. Use git config --local --list to check the local configuration, or use git config --show-origin user.name to view the configuration source.
Q How do I delete a configuration item?
A Use git config --global --unset 配置项名称—for example, git config --global --unset user.name—to delete the global username configuration.
Q Can configuration files be edited manually?
A Yes. The configuration file is a plain text file, so you can open and edit it directly in a text editor. However, we recommend using the git config command to avoid syntax errors.

📖 Summary


📝 Exercises

  1. Basic Exercise (Difficulty: ⭐): Configure your global Git credentials (username and email address), and verify that the configuration was successful.

  2. 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.

  3. 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.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏