# 🚀 Git 命令大全:从入门到精通的完整指南

3 阅读9分钟

作为现代软件开发的必备技能,Git 已经成为每个开发者工具箱中的核心组件。但面对 Git 庞大的命令体系,很多开发者都感到困惑:到底有哪些命令?如何正确使用?什么时候该用哪个命令?

今天,我将为你带来一份史上最全的 Git 命令指南,涵盖所有官方命令分类,配合实用示例和最佳实践,让你彻底掌握 Git 的精髓!


📋 Git 命令分类总览

根据 Git 官方文档,Git 命令被分为以下 8 大类别

分类命令数量主要用途
🔧 设置与配置10+环境配置、用户信息设置
📥 获取与创建项目5+初始化仓库、克隆项目
📸 快照基础操作15+文件跟踪、提交管理
🌿 分支与合并20+分支管理、代码合并
🌐 项目分享与更新15+远程仓库操作
🔍 检查与比较20+状态查看、差异对比
🐞 调试与修复10+问题定位、历史回溯
⚙️ 底层 Plumbing 命令50+高级操作、内部机制

💡 小知识:Git 命令分为 Porcelain(瓷器)Plumbing(管道) 两类

  • Porcelain:面向用户的高级命令(如 git commit
  • Plumbing:面向脚本的底层命令(如 git hash-object

🔧 一、设置与配置(Setup and Config)

核心命令

git config - 配置 Git 行为

# 全局配置(推荐)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 本地仓库配置
git config user.name "Project Specific Name"

# 查看配置
git config --list                    # 查看所有配置
git config --global --list          # 查看全局配置
git config user.name                # 查看特定配置

# 高级配置
git config --global core.editor vim          # 设置默认编辑器
git config --global init.defaultBranch main  # 设置默认分支名
git config --global color.ui auto            # 启用彩色输出

git help - 获取帮助

git help <command>        # 查看命令帮助
git help config           # 查看 git config 帮助
git help -a               # 列出所有可用命令
git help -g               # 列出概念性帮助指南

📥 二、获取与创建项目(Getting and Creating Projects)

核心命令

git init - 初始化新仓库

git init                    # 在当前目录初始化
git init my-project         # 创建新目录并初始化
git init --bare             # 创建裸仓库(用于服务器)

git clone - 克隆远程仓库

git clone <repository>                    # 克隆到同名目录
git clone <repository> <directory>        # 克隆到指定目录
git clone --depth 1 <repository>          # 浅克隆(只获取最新提交)
git clone --branch <branch> <repository>  # 克隆特定分支
git clone --recursive <repository>        # 递归克隆子模块

📸 三、快照基础操作(Basic Snapshotting)

核心命令

git add - 添加文件到暂存区

git add <file>              # 添加单个文件
git add .                   # 添加当前目录所有更改
git add -A                  # 添加所有更改(包括删除)
git add -u                  # 添加已跟踪文件的更改
git add -p                  # 交互式添加(逐块选择)

git status - 查看工作区状态

git status                  # 详细状态
git status -s               # 简洁状态(短格式)
git status --ignored        # 显示被忽略的文件

git diff - 查看差异

git diff                    # 工作区 vs 暂存区
git diff --cached           # 暂存区 vs 最后提交
git diff HEAD               # 工作区 vs 最后提交
git diff <commit1> <commit2> # 两个提交之间的差异
git diff --stat             # 统计差异(显示修改行数)

git commit - 提交更改

git commit -m "message"     # 提交并添加消息
git commit                  # 打开编辑器写详细消息
git commit -a -m "message"  # 跳过 add 步骤(仅限已跟踪文件)
git commit --amend          # 修改最近一次提交
git commit --amend --no-edit # 修改提交但保留原消息

git reset - 重置提交

git reset --soft HEAD~1     # 保留更改在暂存区
git reset --mixed HEAD~1    # 保留更改在工作区(默认)
git reset --hard HEAD~1     # 完全丢弃更改(危险!)
git reset <file>            # 从暂存区移除文件

git rm - 删除文件

git rm <file>               # 删除并暂存删除操作
git rm --cached <file>      # 从版本控制中移除但保留本地文件
git rm -r <directory>       # 递归删除目录

git mv - 移动/重命名文件

git mv <old> <new>          # 移动或重命名文件

🌿 四、分支与合并(Branching and Merging)

核心命令

git branch - 分支管理

git branch                  # 列出所有本地分支
git branch -a               # 列出所有分支(包括远程)
git branch <name>           # 创建新分支
git branch -d <name>        # 删除分支
git branch -D <name>        # 强制删除分支
git branch -m <old> <new>   # 重命名分支

git checkout / git switch - 切换分支

# 传统方式(仍广泛使用)
git checkout <branch>       # 切换到分支
git checkout -b <branch>    # 创建并切换到新分支

# 新方式(Git 2.23+ 推荐)
git switch <branch>         # 切换到分支
git switch -c <branch>      # 创建并切换到新分支

git merge - 合并分支

git merge <branch>          # 快进合并或三方合并
git merge --no-ff <branch>  # 禁用快进,创建合并提交
git merge --squash <branch> # 压缩合并(不创建合并提交)

git rebase - 变基操作

git rebase <branch>         # 将当前分支变基到指定分支
git rebase -i <commit>      # 交互式变基(整理提交历史)
git rebase --continue       # 解决冲突后继续变基
git rebase --abort          # 放弃变基操作

git cherry-pick - 挑选提交

git cherry-pick <commit>    # 将指定提交应用到当前分支
git cherry-pick <commit1>..<commit2> # 挑选范围内的提交

🌐 五、项目分享与更新(Sharing and Updating Projects)

核心命令

git remote - 远程仓库管理

git remote                  # 列出远程仓库
git remote -v               # 列出远程仓库(显示URL)
git remote add <name> <url> # 添加远程仓库
git remote remove <name>    # 删除远程仓库
git remote rename <old> <new> # 重命名远程仓库
git remote set-url <name> <newurl> # 更新远程仓库URL

git fetch - 获取远程更新

git fetch                   # 获取所有远程更新
git fetch <remote>          # 获取指定远程仓库更新
git fetch <remote> <branch> # 获取指定分支更新

git pull - 拉取并合并

git pull                    # 等同于 git fetch + git merge
git pull --rebase           # 等同于 git fetch + git rebase
git pull <remote> <branch>  # 拉取指定远程分支

git push - 推送到远程

git push                    # 推送当前分支
git push <remote> <branch>  # 推送指定分支
git push -u <remote> <branch> # 推送并设置上游跟踪
git push --force            # 强制推送(危险!)
git push --force-with-lease # 安全的强制推送
git push --delete <remote> <branch> # 删除远程分支

🔍 六、检查与比较(Inspection and Comparison)

核心命令

git log - 查看提交历史

git log                     # 完整提交历史
git log --oneline           # 单行显示
git log --graph             # 图形化显示分支历史
git log --all               # 显示所有分支历史
git log --author="name"     # 按作者过滤
git log --grep="keyword"    # 按提交消息过滤
git log --since="1 week ago" # 按时间过滤
git log -p                  # 显示每次提交的差异
git log --stat              # 显示统计信息

git show - 显示对象信息

git show <commit>           # 显示提交详情
git show <tag>              # 显示标签详情
git show <file>             # 显示文件内容

git blame - 查看文件修改历史

git blame <file>            # 显示每行的最后修改者
git blame -L 10,20 <file>   # 显示指定行范围

git reflog - 查看引用日志

git reflog                  # 查看 HEAD 的所有变更
git reflog show <branch>    # 查看指定分支的引用日志

🐞 七、调试与修复(Debugging and Fixing)

核心命令

git bisect - 二分查找问题

git bisect start            # 开始二分查找
git bisect good <commit>    # 标记好提交
git bisect bad <commit>     # 标记坏提交
git bisect reset            # 结束二分查找

git stash - 临时保存更改

git stash                   # 保存当前更改
git stash list              # 列出所有暂存
git stash apply             # 应用最近暂存(保留暂存)
git stash pop               # 应用最近暂存(删除暂存)
git stash drop              # 删除暂存
git stash clear             # 清空所有暂存

git clean - 清理未跟踪文件

git clean -n                # 预览将要删除的文件
git clean -f                # 删除未跟踪文件
git clean -fd               # 删除未跟踪文件和目录
git clean -fx               # 删除未跟踪文件(包括忽略的)

git revert - 创建反向提交

git revert <commit>         # 创建撤销指定提交的新提交
git revert <commit1>..<commit2> # 撤销范围内的提交

🏷️ 八、标签管理(Tagging)

核心命令

git tag - 标签操作

git tag                     # 列出所有标签
git tag <name>              # 创建轻量标签
git tag -a <name> -m "msg"  # 创建带注释的标签
git tag -d <name>           # 删除标签
git push origin <tag>       # 推送标签到远程
git push origin --tags      # 推送所有标签

🎯 实用组合命令(Pro Tips)

日常开发工作流

# 标准提交流程
git status
git add .
git commit -m "feat: add new feature"
git pull --rebase origin main
git push origin main

# 修复最近提交
git add .
git commit --amend --no-edit
git push --force-with-lease

# 临时切换任务
git stash
git checkout main
# ... do other work ...
git checkout feature-branch
git stash pop

查看历史的高级技巧

# 图形化显示所有分支历史
git log --all --graph --oneline --decorate

# 查看某个文件的修改历史
git log --follow -p -- <filename>

# 查看两个分支的差异
git log branch1..branch2

# 查看尚未推送到远程的提交
git log origin/main..main

清理和维护

# 清理已合并的本地分支
git branch --merged | grep -v "\*\|main\|master" | xargs -n 1 git branch -d

# 清理远程已删除的分支引用
git remote prune origin

# 压缩仓库大小
git gc --aggressive --prune=now

🛠️ 配置别名提升效率

~/.gitconfig 中添加常用别名:

[alias]
    st = status -s
    co = checkout
    br = branch
    mg = merge
    ci = commit
    df = diff
    lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
    unstage = reset HEAD --
    last = log -1 HEAD
    visual = !gitk

使用示例:

git st      # 等同于 git status -s
git lg      # 美观的图形化日志

⚠️ 常见陷阱与最佳实践

危险操作警告

  • git push --force:可能覆盖他人工作,优先使用 --force-with-lease
  • git reset --hard:会永久丢失未提交的更改
  • git clean -f:会永久删除未跟踪文件

最佳实践建议

  1. 频繁提交:小而频繁的提交比大而稀少的提交更好
  2. 有意义的提交消息:遵循 Conventional Commits 规范
  3. 分支策略:使用 Git Flow 或 GitHub Flow 等成熟策略
  4. 定期同步:经常 git pull --rebase 保持分支更新
  5. 备份重要工作:在进行危险操作前先推送或暂存

📚 学习资源推荐

  • 官方文档git-scm.com/docs
  • 交互式学习Learn Git Branching
  • 书籍推荐:《Pro Git》(免费在线版)
  • 可视化工具:GitKraken、Sourcetree、GitHub Desktop

💡 总结

Git 虽然命令众多,但掌握了这 8 大分类的核心命令,你就已经具备了处理 95% 日常开发场景的能力。记住:

Git 不是魔法,而是工具。理解其背后的原理,比死记硬背命令更重要。

建议你:

  1. 从基础命令开始addcommitpushpull
  2. 逐步学习高级功能rebasecherry-pickbisect
  3. 在实际项目中练习:理论结合实践才能真正掌握
  4. 不要害怕犯错:Git 的设计就是让你可以安全地尝试和回退

现在就开始你的 Git 之旅吧! 🚀


如果你觉得这份 Git 命令大全对你有帮助,欢迎点赞、收藏、转发!也欢迎在评论区分享你的 Git 使用技巧和经验~