View and modify configuration
View Current Configuration
Git configurations are divided into three levels: system (--system
), user (--global
), and repository (--local
). Use the git config
command with the --list
parameter to view configurations:
# View all configurations (includes all three levels)
git config --list
# View system-level configurations
git config --system --list
# View user-level configurations
git config --global --list
# View current repository configurations
git config --local --list
When duplicate configurations exist, repository-level configurations override user-level configurations, and user-level configurations override system-level configurations. For example, if the following configurations exist simultaneously:
# System-level configuration
git config --system user.name "System Name"
# User-level configuration
git config --global user.name "Global Name"
# Repository-level configuration
git config --local user.name "Local Name"
The actual effective value is Local Name
. To view the effective value of a specific configuration item:
git config user.name
Modify Configuration Items
Modify configurations using the git config
command with level parameters and key-value pairs:
# Set user-level configuration
git config --global user.email "your@email.com"
# Set current repository configuration
git config --local core.editor "code --wait"
Common configuration items include:
- User information:
user.name
,user.email
- Default editor:
core.editor
- Line ending handling:
core.autocrlf
- Aliases:
alias
For example, to automatically convert line endings on commit (suitable for Windows):
git config --global core.autocrlf true
Configure Aliases
Git supports creating shortcuts for commonly used commands. For example, creating co
as an alias for checkout
:
git config --global alias.co checkout
After this, you can use git co
instead of git checkout
. More complex aliases can include parameters:
git config --global alias.lg "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)'"
This alias displays a beautifully formatted log output with a branch graph.
Multi-Configuration Management
Developers may need different configurations for different projects. For example, using different emails for company and personal projects:
# Global configuration (for personal projects)
git config --global user.email "personal@email.com"
# Set local configuration after entering the company project directory
cd company-project
git config --local user.email "work@company.com"
Conditional configurations can be used to automate switching. Add the following to ~/.gitconfig
:
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
Then configure work-specific settings in ~/work/.gitconfig
:
[user]
email = work@company.com
Configuration File Locations
Configuration files for different levels are stored in the following locations:
- System-level:
/etc/gitconfig
(Unix) orGit installation directory/etc/gitconfig
(Windows) - User-level:
~/.gitconfig
or~/.config/git/config
- Repository-level:
.git/config
You can directly edit these files to modify configurations. For example, to edit user-level configurations:
# Open with the default editor
git config --global --edit
# Or edit the file directly
vim ~/.gitconfig
Configuration files use the INI format. Example content:
[user]
name = Your Name
email = your@email.com
[core]
editor = code --wait
[alias]
st = status
co = checkout
Temporarily Override Configurations
Sometimes you need to temporarily override a configuration item. Use the -c
parameter:
git -c user.name="Temp Name" commit -m "message"
This is particularly useful in automation scripts to avoid modifying persistent configurations.
Verify Configuration Priority
To verify the final effective value of a configuration item, use:
git config --show-origin user.email
This displays the source file and location of the configuration value, for example:
file:/home/user/.gitconfig user.email=personal@email.com
Delete Configuration Items
Delete configurations using the --unset
parameter:
# Delete user-level configuration
git config --global --unset user.name
# Delete repository-level configuration
git config --local --unset core.editor
To delete all matching items (when multiple duplicate configurations exist), use --unset-all
:
git config --unset-all alias.st
Common Configuration Examples
A typical developer's global configuration might include:
# Basic user information
git config --global user.name "Developer"
git config --global user.email "dev@example.com"
# Set VS Code as the default editor
git config --global core.editor "code --wait"
# Line ending handling (choose based on system)
git config --global core.autocrlf input # Mac/Linux
git config --global core.autocrlf true # Windows
# Colorful output
git config --global color.ui auto
# Common aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn