Git 命令完全手册

Git 命令完全手册

Git 命令完全手册

目录

  1. Git 基础配置
  2. 仓库操作
  3. 核心常用命令
  4. 分支操作
  5. 远程协作
  6. 查看信息
  7. 撤销与回退
  8. 标签管理
  9. 高级操作
  10. 故障排查

1. Git 基础配置

# 查看配置
git config --list
git config --global --list# 设置用户信息(必需)
git config --global user.name "你的名字"
git config --global user.email "your.email@example.com"# 设置文本编辑器
git config --global core.editor "code --wait"  # VS Code
git config --global core.editor "vim"          # Vim# 设置别名(提高效率)
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'# 设置缓存密码(避免重复输入)
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'

2. 仓库操作

# 初始化新仓库
git init                   # 在当前目录创建
git init <directory>       # 在指定目录创建# 克隆远程仓库(常用)
git clone <url>                           # 默认克隆主分支
git clone <url> <directory>               # 克隆到指定目录
git clone -b <branch> <url>               # 克隆指定分支
git clone --depth 1 <url>                 # 浅克隆(只下载最新版本,更快)
git clone --recursive <url>               # 递归克隆子模块# 查看仓库状态
git status                 # 查看工作区状态
git status -s              # 简短格式

3. 核心常用命令

3.1 添加文件到暂存区

git add <file>             # 添加指定文件
git add .                  # 添加所有修改(不包括删除)
git add -A                 # 添加所有变化(包括删除)
git add -u                 # 添加已跟踪文件的修改
git add -p                 # 交互式添加(逐个确认)

3.2 提交更改(常用)

git commit -m "提交信息"                     # 提交暂存区内容
git commit -a -m "提交信息"                  # 跳过add,直接提交已跟踪文件的修改
git commit                               # 打开编辑器编写详细提交信息
git commit -amend                        # 修改最后一次提交(可以改信息或加文件)
git commit -amend -m "新的提交信息"        # 直接修改提交信息
git commit -amend --no-edit              # 只添加文件,不修改信息

3.3 拉取更新(常用)

git pull                   # 拉取并合并远程更新(相当于 fetch + merge)
git pull --rebase          # 拉取并使用rebase方式合并(推荐,历史更干净)
git pull origin main       # 指定远程和分支

3.4 推送代码(常用)

git push                   # 推送到远程
git push -u origin main    # 首次推送并设置上游分支
git push --force           # 强制推送(谨慎使用)
git push --force-with-lease # 更安全的强制推送
git push origin <branch>   # 推送指定分支
git push --all             # 推送所有分支
git push --tags            # 推送所有标签

3.5 克隆仓库(常用)

git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git      # SSH方式(推荐)
git clone -b develop https://github.com/user/repo.git  # 克隆develop分支

4. 分支操作

# 查看分支
git branch                 # 查看本地分支
git branch -a              # 查看所有分支(本地+远程)
git branch -r              # 只查看远程分支
git branch -v              # 查看分支详细信息# 创建与切换分支
git branch <branch>        # 创建新分支
git checkout <branch>      # 切换到已有分支
git checkout -b <branch>   # 创建并切换到新分支(常用)
git switch <branch>        # Git 2.23+ 新命令,切换分支
git switch -c <branch>     # Git 2.23+ 创建并切换分支# 合并分支
git merge <branch>         # 合并指定分支到当前分支
git merge --no-ff <branch> # 禁用Fast-forward合并,保留分支历史
git merge --abort          # 中止合并# 删除分支
git branch -d <branch>     # 删除已合并的分支
git branch -D <branch>     # 强制删除分支(未合并的也删)# 重命名分支
git branch -m <old> <new>  # 重命名当前分支
git branch -M <old> <new>  # 强制重命名# 比较分支
git diff <branch1> <branch2>           # 比较两个分支差异
git diff <branch1> <branch2> -- <file> # 比较指定文件的差异

5. 远程协作

# 查看远程仓库
git remote                 # 查看远程仓库名称
git remote -v              # 查看远程仓库详细信息# 管理远程仓库
git remote add origin <url>     # 添加远程仓库
git remote remove origin        # 删除远程仓库
git remote rename <old> <new>   # 重命名远程仓库
git remote set-url origin <url> # 修改远程仓库URL# 获取远程更新
git fetch                  # 获取所有远程更新(不合并)
git fetch origin           # 获取指定远程更新
git fetch --prune          # 获取更新并清理已删除的远程分支# 推送与拉取
git push -u origin <branch> # 推送并设置上游分支
git pull --rebase          # 拉取并使用rebase合并# 跟踪远程分支
git branch --track <local> <remote>/<branch>  # 创建跟踪分支
git branch --set-upstream-to=origin/<branch>  # 设置当前分支跟踪远程分支
git checkout --track origin/<branch>          # 创建并切换到跟踪远程分支的分支

6. 查看信息

# 查看提交历史
git log                    # 查看完整历史
git log --oneline          # 简洁模式(一行一个提交)
git log --graph            # 图形化显示分支合并历史
git log --graph --oneline --decorate --all  # 综合显示(推荐)
git log -n 5               # 显示最近5条
git log --author="名字"    # 按作者筛选
git log --since="2 days ago"  # 按时间筛选
git log --grep="关键词"    # 按提交信息筛选
git log -- <file>          # 查看指定文件的历史
git log -p                 # 显示每次提交的差异
git log --stat             # 显示统计信息# 查看差异
git diff                   # 查看工作区与暂存区的差异
git diff --cached          # 查看暂存区与仓库的差异
git diff HEAD              # 查看工作区与仓库的差异
git diff <commit1> <commit2>  # 比较两次提交# 查看文件状态
git status                 # 查看状态(前文已述)# 查看某次提交详情
git show <commit>          # 显示某次提交的详细信息
git show <commit>:<file>   # 显示某次提交中的某个文件内容

7. 撤销与回退

# 撤销工作区修改
git checkout -- <file>     # 撤销工作区修改(危险操作)
git restore <file>         # Git 2.23+ 新命令# 撤销暂存区修改
git reset HEAD <file>      # 将文件从暂存区撤出
git restore --staged <file> # Git 2.23+ 新命令# 回退版本
git reset --soft <commit>  # 只回退commit,保留暂存区和工作区
git reset --mixed <commit> # 回退commit和暂存区,保留工作区(默认)
git reset --hard <commit>  # 全部回退(危险操作)# 使用reflog找回丢失的提交
git reflog                 # 查看所有操作记录
git reset --hard <commit>  # 通过reflog找到的commit ID恢复# 撤销已推送的提交(推荐使用revert)
git revert <commit>        # 创建一个新提交来撤销指定提交(安全)
git revert -n <commit>     # 撤销但不自动提交# 暂存与恢复
git stash                  # 暂存当前修改
git stash push -m "说明"   # 带说明的暂存
git stash list             # 查看暂存列表
git stash pop              # 恢复最近的一次暂存并删除
git stash apply            # 恢复最近的一次暂存但不删除
git stash drop             # 删除最近的一次暂存
git stash clear            # 清空所有暂存

8. 标签管理

# 查看标签
git tag                    # 列出所有标签
git tag -l "v1.*"          # 按通配符查找标签# 创建标签
git tag <tagname>          # 创建轻量标签
git tag -a <tagname> -m "说明"  # 创建带附注的标签
git tag -a <tagname> <commit>   # 给历史提交打标签# 推送标签
git push origin <tagname>  # 推送单个标签
git push --tags            # 推送所有标签# 删除标签
git tag -d <tagname>       # 删除本地标签
git push origin :refs/tags/<tagname>  # 删除远程标签# 检出标签
git checkout <tagname>     # 检出到标签状态(游离头指针)
git checkout -b <branch> <tagname>  # 创建分支并检出标签

9. 高级操作

# 变基操作(整理提交历史)
git rebase <branch>        # 将当前分支变基到指定分支
git rebase -i HEAD~3       # 交互式变基最近3次提交
git rebase --abort         # 中止变基
git rebase --continue      # 继续变基(解决冲突后)# Cherry-pick(挑选提交)
git cherry-pick <commit>   # 将指定提交应用到当前分支
git cherry-pick --abort    # 中止操作# 清理与优化
git gc                     # 垃圾回收,清理无用文件
git fsck                   # 检查仓库完整性
git prune                  # 清理无效对象# 子模块管理
git submodule add <url>    # 添加子模块
git submodule init         # 初始化子模块
git submodule update       # 更新子模块
git submodule sync         # 同步子模块URL# 工作树(Worktree)
git worktree add <path> <branch>  # 添加工作树
git worktree remove <path>        # 删除工作树
git worktree list                 # 列出工作树# 搜索
git grep "关键词"           # 在仓库中搜索
git grep -n "关键词"        # 显示行号
git grep --cached "关键词"  # 在暂存区搜索

10. 故障排查

# 查看Git状态
git status                 # 查看当前状态# 查看操作日志
git reflog                 # 查看所有操作记录(救命命令)# 恢复丢失的提交
git fsck --lost-found      # 查找丢失的对象# 处理冲突
git status                 # 查看冲突文件
# 手动编辑冲突文件后
git add <file>             # 标记冲突已解决
git commit                 # 完成合并# 诊断
git --version              # 查看Git版本
git help <command>         # 查看命令帮助
git <command> --help       # 同上# 调试
git config --list          # 查看配置
git config --global --edit # 编辑全局配置# 更换远程URL
git remote set-url origin <new-url>  # 更换远程地址

附录:Git 工作流速查

日常开发流程

# 1. 克隆或更新代码
git clone <url>            # 首次
git pull --rebase          # 日常更新# 2. 创建功能分支
git checkout -b feature/xxx# 3. 编码并提交
git add .
git commit -m "feat: 添加新功能"# 4. 推送代码
git push -u origin feature/xxx# 5. 合并到主分支(PR/MR或命令行)
git checkout main
git merge feature/xxx
git push

紧急修复流程

# 1. 从主分支创建修复分支
git checkout main
git pull --rebase
git checkout -b hotfix/bug# 2. 修复并提交
git add .
git commit -m "fix: 修复严重bug"# 3. 测试后合并
git checkout main
git merge hotfix/bug
git tag v1.0.1
git push origin main --tags

提示

  • 使用 git help <command> 查看任何命令的详细帮助
  • 配置好 .gitignore 文件避免提交不必要的文件
  • 定期使用 git pull --rebase 保持代码同步
  • 提交信息遵循规范(如 Conventional Commits)
  • 重要操作前建议先创建备份分支

本手册基于 Git 2.23+ 版本编写,部分新命令(如 switchrestore)在旧版本中不可用,可使用传统命令替代。