📚 Git 速查表
开发人员完整的 Git 命令参考。搜索并复制 git 命令,包括提交、推送、拉取、分支、合并等。
💡 使用提示: 点击命令旁边的"复制"按钮快速复制,点击类别标题可展开/折叠内容。
⚙️ 配置
▼
git config --global user.name "[name]"
设置全局用户名
git config --global user.email "[email]"
设置全局邮箱
git config --global core.editor "[editor]"
设置默认编辑器
git config --global color.ui true
启用彩色输出
git config --global alias.co checkout
设置命令别名 (checkout → co)
🚀 创建仓库
▼
git init
初始化新的 Git 仓库
git clone [url]
克隆远程仓库到本地
git clone -b [branch] [url]
克隆指定分支
💾 基本提交
▼
git status
查看工作目录状态
git add [file]
添加文件到暂存区
git add .
添加所有修改的文件
git commit -m "[message]"
提交暂存的内容
git commit -am "[message]"
提交所有跟踪的更改
git commit --amend
修改最后一次提交
git commit --amend --no-edit
修改提交但不改变消息
🌿 分支管理
▼
git branch
查看本地分支列表
git branch -a
查看所有分支(包括远程)
git branch [branch-name]
创建新分支
git checkout [branch-name]
切换到指定分支
git checkout -b [branch-name]
创建并切换到新分支
git branch -d [branch-name]
删除已合并的分支
git branch -D [branch-name]
强制删除分支
git branch -m [old-name] [new-name]
重命名分支
☁️ 远程操作
▼
git remote -v
查看远程仓库信息
git remote add origin [url]
添加远程仓库
git pull [remote] [branch]
拉取远程更新
git push [remote] [branch]
推送本地分支
git push -f [remote] [branch]
强制推送(覆盖远程)
git fetch [remote]
获取远程更新(不合并)
git remote remove [remote]
删除远程仓库
🔀 合并与变基
▼
git merge [branch]
合并指定分支到当前分支
git merge --no-ff [branch]
合并但创建合并提交
git rebase [branch]
变基到指定分支
git rebase --continue
继续变基操作
git rebase --abort
中止变基操作
↩️ 撤销与回滚
▼
git reset HEAD~1
撤销最后一次提交(保留更改)
git reset HEAD~[N]
撤销最后 N 次提交
git reset --hard HEAD~1
撤销提交并丢弃更改
git reset --hard [commit-hash]
回滚到指定提交
git checkout -- [file]
丢弃本地文件更改
git revert [commit-hash]
创建新提交来撤销指定提交
git clean -fd
删除未跟踪的文件和目录
🔍 查看历史
▼
git log
查看提交历史
git log --oneline
简洁的单行历史
git log --graph --oneline
图形化分支历史
git log --stat
查看提交统计
git diff
查看工作目录差异
git diff --staged
查看暂存区差异
git show [commit-hash]
查看指定提交详情
🏷️ 标签管理
▼
git tag
查看标签列表
git tag [tag-name]
创建轻量标签
git tag -a [tag-name] -m "[message]"
创建附注标签
git push [remote] [tag-name]
推送标签到远程
git push [remote] --tags
推送所有标签
git tag -d [tag-name]
删除本地标签
📦 储藏与暂存
▼
git stash
储藏当前更改
git stash save "[message]"
带消息储藏
git stash list
查看储藏列表
git stash apply
恢复储藏(不删除)
git stash pop
恢复储藏并删除
git stash drop
删除最新储藏
git stash clear
清空所有储藏
🧩 子模块
▼
git submodule add [url] [path]
添加子模块
git submodule init
初始化子模块
git submodule update
更新子模块
⚡ 高级技巧
▼
git cherry-pick [commit-hash]
挑选并应用指定提交
git bisect start [bad] [good]
二分查找定位问题
git blame [file]
查看文件修改历史
git reflog
查看所有操作记录
git clean -xdf
彻底清理工作目录(谨慎使用)
git gc --aggressive
压缩并优化仓库