Git速查表
Git命令速查表,涵盖初始化、分支管理、合并、远程操作、撤销、标签、暂存等常用Git命令及说明。
⚙️ 基础配置
| 命令 | 说明 |
|---|---|
git config --global user.name "name" | 设置全局用户名 |
git config --global user.email "email" | 设置全局邮箱 |
git config --list | 查看所有配置 |
git init | 初始化本地Git仓库 |
git init --bare | 初始化裸仓库(用于服务器) |
git clone <url> | 克隆远程仓库到本地 |
git clone --depth 1 <url> | 浅克隆,只获取最新一次提交 |
📋 日常操作
| 命令 | 说明 |
|---|---|
git add <file> | 将文件添加到暂存区 |
git add . | 将当前目录所有更改添加到暂存区 |
git add -A | 将所有更改(包括删除)添加到暂存区 |
git commit -m "message" | 提交暂存区内容 |
git commit -am "message" | 跳过add,直接提交已跟踪文件的更改 |
git status | 查看工作区和暂存区状态 |
git status -s | 简洁模式显示状态 |
git log | 查看提交历史 |
git log --oneline | 单行简洁显示提交历史 |
git log --graph --oneline --all | 图形化显示所有分支提交历史 |
git diff | 查看工作区与暂存区的差异 |
git diff --staged | 查看暂存区与上次提交的差异 |
git diff <branch1> <branch2> | 查看两个分支的差异 |
git show <commit> | 查看某次提交的详细内容 |
git show --stat <commit> | 查看某次提交的文件变更统计 |
🔀 分支管理
| 命令 | 说明 |
|---|---|
git branch | 查看本地分支列表 |
git branch -a | 查看所有分支(含远程) |
git branch <name> | 创建新分支 |
git branch -d <name> | 删除已合并的分支 |
git branch -D <name> | 强制删除分支 |
git branch -m <old> <new> | 重命名分支 |
git checkout <branch> | 切换到指定分支 |
git checkout -b <branch> | 创建并切换到新分支 |
git switch <branch> | 切换分支(推荐,Git 2.23+) |
git switch -c <branch> | 创建并切换到新分支(推荐) |
git merge <branch> | 将指定分支合并到当前分支 |
git merge --no-ff <branch> | 合并时创建合并提交(不快进) |
git rebase <branch> | 将当前分支变基到指定分支 |
git rebase --abort | 中止变基操作 |
git rebase --continue | 解决冲突后继续变基 |
🌍 远程操作
| 命令 | 说明 |
|---|---|
git remote -v | 查看远程仓库列表 |
git remote add <name> <url> | 添加远程仓库 |
git remote remove <name> | 删除远程仓库 |
git remote rename <old> <new> | 重命名远程仓库 |
git push <remote> <branch> | 推送本地分支到远程 |
git push -u origin <branch> | 推送并设置上游跟踪分支 |
git push --force | 强制推送(覆盖远程历史) |
git push --all | 推送所有本地分支 |
git pull | 拉取并合并远程当前分支 |
git pull --rebase | 拉取后变基(保持线性历史) |
git fetch | 获取远程最新状态(不合并) |
git fetch --prune | 获取并清理已删除的远程分支 |
⏪ 撤销操作
| 命令 | 说明 |
|---|---|
git reset --soft HEAD~1 | 回退提交,保留更改在暂存区 |
git reset --mixed HEAD~1 | 回退提交,保留更改在工作区 |
git reset --hard HEAD~1 | 回退提交,丢弃所有更改 |
git reset HEAD <file> | 取消文件的暂存状态 |
git revert <commit> | 创建新提交来撤销指定提交 |
git revert HEAD | 撤销最近一次提交 |
git stash | 暂存当前工作区更改 |
git stash push -m "message" | 带说明暂存更改 |
git stash list | 查看所有暂存记录 |
git stash pop | 恢复最近一次暂存并删除记录 |
git stash apply | 恢复最近一次暂存(保留记录) |
git stash drop | 删除最近一次暂存记录 |
git stash clear | 清空所有暂存记录 |
git checkout -- <file> | 丢弃工作区对文件的修改 |
git restore <file> | 恢复文件到上次提交状态(推荐,Git 2.23+) |
🏷️ 标签管理
| 命令 | 说明 |
|---|---|
git tag | 查看所有标签 |
git tag <name> | 在当前提交创建轻量标签 |
git tag -a <name> -m "message" | 创建附注标签 |
git tag -a <name> <commit> | 在指定提交创建标签 |
git tag -d <name> | 删除本地标签 |
git push origin <tag> | 推送指定标签到远程 |
git push origin --tags | 推送所有本地标签到远程 |
git push origin --delete <tag> | 删除远程标签 |
git checkout <tag> | 切换到指定标签 |
🔬 高级操作
| 命令 | 说明 |
|---|---|
git cherry-pick <commit> | 将指定提交应用到当前分支 |
git cherry-pick <commit1> <commit2> | 选择多个提交应用 |
git bisect start | 启动二分查找定位引入bug的提交 |
git bisect bad | 标记当前提交为有问题 |
git bisect good <commit> | 标记指定提交为正常 |
git bisect reset | 结束二分查找,返回原分支 |
git reflog | 查看所有引用历史(找回丢失提交) |
git reflog show <branch> | 查看指定分支的引用历史 |
git blame <file> | 逐行查看文件的修改历史和作者 |
git blame -L 10,20 <file> | 查看指定行范围的修改历史 |
git clean -fd | 删除未跟踪的文件和目录 |
git clean -fdn | 预览将被清理的文件(不实际删除) |
git shortlog -sn | 按提交数统计各贡献者 |
git archive --format=zip HEAD -o archive.zip | 将当前提交导出为zip压缩包 |
关于Git
Git是目前世界上最先进的分布式版本控制系统。掌握这些常用命令可以显著提高你的开发效率。建议配合.gitignore文件和Git工作流(如Git Flow或GitHub Flow)使用。