Git 使用三个级别的配置文件,优先级从高到低:
- 系统级配置:/etc/gitconfig - 适用于系统所有用户
- 全局配置:~/.gitconfig 或 ~/.config/git/config - 适用于当前用户的所有仓库
- 本地配置:.git/config - 仅适用于当前仓库
全面掌握 Git 配置与常用操作
Git 使用三个级别的配置文件,优先级从高到低:
每个 Git 仓库的 .git 目录包含以下重要文件:
使用 git config --list --show-origin 可以查看所有配置及其来源位置。
# 查看所有配置
git config --list
# 查看特定配置项
git config user.name
git config user.email
# 查看配置来源
git config --list --show-origin
# 查看全局配置
git config --global --list
# 查看本地配置
git config --local --list
# 设置全局用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 设置本地仓库配置
git config --local user.name "Project Name"
git config --local user.email "project@example.com"
# 设置编辑器
git config --global core.editor "vim"
git config --global core.editor "code --wait"
# 设置默认分支名
git config --global init.defaultBranch main
| 配置项 | 说明 | 示例 |
|---|---|---|
user.name |
提交者姓名 | git config --global user.name "张三" |
user.email |
提交者邮箱 | git config --global user.email "zhangsan@example.com" |
core.editor |
默认编辑器 | git config --global core.editor "vim" |
core.autocrlf |
换行符处理 | git config --global core.autocrlf true |
alias.* |
命令别名 | git config --global alias.st status |
push.default |
推送默认行为 | git config --global push.default simple |
# 删除全局配置
git config --global --unset user.name
# 删除本地配置
git config --local --unset user.email
# 删除系统配置(需要管理员权限)
git config --system --unset core.editor
Git 配置文件使用 INI 格式,由节(section)和键值对组成:
[user]
name = Your Name
email = your.email@example.com
[core]
editor = vim
autocrlf = true
filemode = true
[alias]
st = status
co = checkout
br = branch
ci = commit
unstage = reset HEAD --
last = log -1 HEAD
visual = !gitk
[push]
default = simple
[color]
ui = auto
branch = auto
diff = auto
节名格式:[section "subsection"]
[user][remote "origin"]# 添加多个远程仓库
[remote "origin"]
url = https://github.com/user/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "upstream"]
url = https://github.com/original/repo.git
fetch = +refs/heads/*:refs/remotes/upstream/*
# 多个 URL(使用 url 而不是 url)
[remote "origin"]
url = https://github.com/user/repo.git
url = git@github.com:user/repo.git
可以直接编辑 ~/.gitconfig 文件来修改配置,但建议使用 git config 命令,可以自动处理格式和验证。
# 查看本地分支
git branch
# 查看所有分支(包括远程)
git branch -a
# 查看远程分支
git branch -r
# 查看分支详细信息
git branch -v
# 查看已合并的分支
git branch --merged
# 查看未合并的分支
git branch --no-merged
# 创建新分支
git branch feature-branch
# 创建并切换到新分支
git checkout -b feature-branch
# 使用新语法创建并切换分支
git switch -c feature-branch
# 基于指定提交创建分支
git branch feature-branch commit-hash
# 基于远程分支创建本地分支
git checkout -b local-branch origin/remote-branch
# 切换到指定分支
git checkout branch-name
# 使用新语法切换分支
git switch branch-name
# 切换到上一个分支
git checkout -
git switch -
# 删除已合并的分支
git branch -d branch-name
# 强制删除分支(即使未合并)
git branch -D branch-name
# 删除远程分支
git push origin --delete branch-name
git push origin :branch-name # 旧语法
# 重命名当前分支
git branch -m new-branch-name
# 重命名指定分支
git branch -m old-name new-name
# 重命名远程分支(需要先删除再推送)
git branch -m old-name new-name
git push origin :old-name
git push origin new-name
git push origin -u new-name # 设置上游分支
删除分支前确保分支已经合并或不再需要,使用 -D 选项会强制删除未合并的分支,可能导致工作丢失。
# 还原单个文件到最新提交状态
git checkout -- filename
git restore filename # 新语法
# 还原所有文件
git checkout -- .
git restore . # 新语法
# 还原到指定提交的文件
git checkout commit-hash -- filename
# 取消暂存单个文件
git reset HEAD filename
git restore --staged filename # 新语法
# 取消暂存所有文件
git reset HEAD
git restore --staged . # 新语法
# 撤销最后一次提交,保留更改在暂存区
git reset --soft HEAD~1
# 撤销最后一次提交,保留更改在工作区
git reset --mixed HEAD~1
git reset HEAD~1 # --mixed 是默认选项
# 撤销最后一次提交,丢弃所有更改
git reset --hard HEAD~1
# 撤销到指定提交
git reset --hard commit-hash
git revert 会创建一个新提交来撤销指定提交的更改,不会改变历史记录:
# 撤销指定提交
git revert commit-hash
# 撤销最后一次提交
git revert HEAD
# 撤销多个提交
git revert commit-hash1 commit-hash2
# 撤销提交但不自动提交
git revert -n commit-hash
| 操作 | 用途 | 是否改变历史 | 适用场景 |
|---|---|---|---|
reset |
回退到指定提交 | 是(本地) | 本地未推送的提交 |
revert |
创建新提交撤销更改 | 否 | 已推送的提交 |
git reset --hard 会永久删除未提交的更改,使用前请确保重要数据已保存。对于已推送到远程的提交,应使用 git revert 而不是 git reset。
# 保存当前工作区和暂存区的更改
git stash
# 保存并添加描述信息
git stash save "描述信息"
# 保存未跟踪的文件
git stash -u
git stash --include-untracked
# 保存所有文件(包括忽略的文件)
git stash -a
git stash --all
# 查看所有 stash
git stash list
# 查看 stash 的详细内容
git stash show
# 查看指定 stash 的详细内容
git stash show stash@{0}
# 查看 stash 的完整差异
git stash show -p stash@{0}
# 应用最新的 stash,保留 stash
git stash apply
# 应用指定的 stash
git stash apply stash@{1}
# 应用最新的 stash,删除 stash
git stash pop
# 应用指定的 stash,删除 stash
git stash pop stash@{1}
# 删除最新的 stash
git stash drop
# 删除指定的 stash
git stash drop stash@{1}
# 删除所有 stash
git stash clear
# 基于 stash 创建新分支
git stash branch new-branch-name stash@{0}
# 基于最新 stash 创建分支
git stash branch new-branch-name
git reflog 记录了所有 HEAD 的移动历史,包括被删除的提交:
# 查看 reflog 历史
git reflog
# 查看指定分支的 reflog
git reflog show branch-name
# 查看最近 N 条记录
git reflog -n 20
# 基于 reflog 恢复提交
git checkout commit-hash
git checkout -b recovered-branch commit-hash
git fsck 可以找到所有悬空(dangling)的对象,包括被删除的提交:
# 查找所有悬空对象
git fsck --lost-found
# 查找悬空的提交
git fsck --unreachable | grep commit
# 查看提交内容
git show commit-hash
# 恢复提交
git checkout commit-hash
git checkout -b recovered-branch commit-hash
# 查看 reflog 找到分支删除前的提交
git reflog
# 基于提交恢复分支
git branch recovered-branch commit-hash
# 或者直接切换并创建分支
git checkout -b recovered-branch commit-hash
# 查看本地 reflog(如果本地还有记录)
git reflog
# 查看远程 reflog(如果配置了)
git reflog origin/branch-name
# 恢复提交
git checkout commit-hash
git checkout -b recovered-branch commit-hash
# 推送到远程
git push origin recovered-branch
# 1. 查看 reflog 找到被删除的提交
git reflog
# 输出示例:
# abc1234 HEAD@{0}: reset: moving to HEAD~1
# def5678 HEAD@{1}: commit: 重要更改
# 2. 查看提交内容确认
git show def5678
# 3. 创建新分支恢复
git checkout -b recovered-branch def5678
# 4. 如果需要合并回主分支
git checkout main
git merge recovered-branch
一旦执行了 git gc(垃圾回收),未引用的对象可能被永久删除。如果数据非常重要,建议立即恢复,不要等待。