🚀 Git 使用指南

全面掌握 Git 配置与常用操作

📁 Git 配置目录介绍

配置文件位置

Git 使用三个级别的配置文件,优先级从高到低:

  • 系统级配置/etc/gitconfig - 适用于系统所有用户
  • 全局配置~/.gitconfig~/.config/git/config - 适用于当前用户的所有仓库
  • 本地配置.git/config - 仅适用于当前仓库

配置目录结构

每个 Git 仓库的 .git 目录包含以下重要文件:

  • config - 仓库配置文件
  • HEAD - 指向当前分支的引用
  • refs/ - 存储分支和标签引用
  • objects/ - Git 对象数据库
  • hooks/ - Git 钩子脚本
  • index - 暂存区索引文件
💡 提示

使用 git config --list --show-origin 可以查看所有配置及其来源位置。

⚙️ Git 配置相关命令

查看配置

# 查看所有配置 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 格式

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)格式

节名格式:[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 分支操作

查看分支

# 查看本地分支 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 还原操作

还原工作区文件

# 还原单个文件到最新提交状态 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

Revert 操作

git revert 会创建一个新提交来撤销指定提交的更改,不会改变历史记录:

# 撤销指定提交 git revert commit-hash # 撤销最后一次提交 git revert HEAD # 撤销多个提交 git revert commit-hash1 commit-hash2 # 撤销提交但不自动提交 git revert -n commit-hash

Reset vs Revert

操作 用途 是否改变历史 适用场景
reset 回退到指定提交 是(本地) 本地未推送的提交
revert 创建新提交撤销更改 已推送的提交
⚠️ 警告

git reset --hard 会永久删除未提交的更改,使用前请确保重要数据已保存。对于已推送到远程的提交,应使用 git revert 而不是 git reset

📦 Git Stash 操作

保存工作进度

# 保存当前工作区和暂存区的更改 git stash # 保存并添加描述信息 git stash save "描述信息" # 保存未跟踪的文件 git stash -u git stash --include-untracked # 保存所有文件(包括忽略的文件) git stash -a git stash --all

查看 Stash 列表

# 查看所有 stash git stash list # 查看 stash 的详细内容 git stash show # 查看指定 stash 的详细内容 git stash show stash@{0} # 查看 stash 的完整差异 git stash show -p stash@{0}

应用 Stash

# 应用最新的 stash,保留 stash git stash apply # 应用指定的 stash git stash apply stash@{1} # 应用最新的 stash,删除 stash git stash pop # 应用指定的 stash,删除 stash git stash pop stash@{1}

删除 Stash

# 删除最新的 stash git stash drop # 删除指定的 stash git stash drop stash@{1} # 删除所有 stash git stash clear

创建分支 from Stash

# 基于 stash 创建新分支 git stash branch new-branch-name stash@{0} # 基于最新 stash 创建分支 git stash branch new-branch-name
💡 使用场景
  • 临时切换分支但不想提交当前更改
  • 需要紧急修复 bug 但当前工作未完成
  • 测试其他分支但保留当前工作进度
  • 清理工作区但保留更改以备后用

🔍 Git 找回已删除的 Commit

使用 reflog 找回

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

使用 fsck 找回

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
💡 重要提示
  • reflog 默认保留 90 天,之后可能被清理
  • 使用 git config gc.reflogExpire 可以调整保留时间
  • 定期备份重要仓库,防止数据丢失
  • 如果提交从未被引用过(如未提交的更改),可能无法找回
⚠️ 注意事项

一旦执行了 git gc(垃圾回收),未引用的对象可能被永久删除。如果数据非常重要,建议立即恢复,不要等待。