文章目錄
- 基本概述
- 配置級別
- 基本用法
- 設置配置項
- 查看配置項
- 刪除配置項
- 常用配置項
基本概述
git config 的作用是:設置用戶信息、編輯器、別名、倉庫行為等。
配置級別
級別 | 作用范圍 | 配置文件路徑 | 命令選項 |
---|---|---|---|
倉庫級別(Local) | 當前倉庫 | .git/config | 無(默認選項) |
全局級別(Global) | 當前用戶 所有倉庫 | ~/.gitconfig(Linux/macOS) 用戶\xxx.gitconfig% (Windows) | - -global |
系統級別(System) | 所有用戶 所有倉庫 | /etc/gitconfig(Linux/macOS) Git安裝目錄\etc\gitconfig(Windows) | - -system |
- 優先級從高到低為:系統 > 全局 > 倉庫
基本用法
設置配置項
1.格式
git config [級別] [選項] <key> <value>
2.例子
# 設置用戶名和郵箱(全局)
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
查看配置項
1.查看所有配置項
# 查看所有配置項(含來源文件)
git config --list --show-origin
2.查看指定配置項
git config --get user.name
3.直接打開文件查看
$ cat .git/config
[core]repositoryformatversion = 0filemode = falsebare = falselogallrefupdates = truesymlinks = falseignorecase = trueeditor = code --wait
[gui]wmstate = normalgeometry = 1205x669+152+152 276 304
[remote "origin"]url = git@gitee.com:xxx1231/xxx.gitfetch = +refs/heads/*:refs/remotes/origin/*
[branch "local_branch"]remote = originmerge = refs/heads/local_branch
[init]defaultBranch = main
刪除配置項
1.刪除全局級別的配置
git config --global --unset user.email
2.刪除所有級別的配置(慎用)
git config --unset-all user.name
常用配置項
1.用戶信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
- 必須配置名字和郵箱才能進行 commit 操作
2.默認文本編輯器
# 設置 VSCode 為默認編輯器
git config --global core.editor "code --wait"# 其他編輯器示例:
# Vim: git config --global core.editor "vim"
# Nano: git config --global core.editor "nano"
3.設置默認分支名稱
git config --global init.defaultBranch main
- 將默認分支從 “master” 改為 “main”
4. 憑證存儲(避免重復輸入密碼)
# 使用緩存(默認15分鐘)
git config --global credential.helper cache# 自定義緩存時間(例如1小時)
git config --global credential.helper "cache --timeout=3600"# 永久存儲(慎用)
git config --global credential.helper store # 明文保存到 ~/.git-credentials
5.配置別名(Alias)
# 簡化常用命令
git config --global alias.st "status"
git config --global alias.br "branch"
git config --global alias.ci "commit"
git config --global alias.co "checkout"
- 配置別名后,輸入 git st ,就相當于輸入了 git status