Git 命令速查手册
文档说明
<> 表示【需替换的项】 [] 表示【非必填项】 | 表示【或】 工作树(工作区),索引(暂存区),Git 目录(HEAD) 三词含义参照 Git 官网
初始配置
git config --global user.name [<username>] # 配置用户名
git config --global user.email [<email>] # 配置邮箱
git config --global core.editor [<vim>] # 配置编辑器
创建项目
git clone <options> # 克隆远程仓库
git init [project] # 初始化本地项目
添加提交
git add <file> # 添加文件到暂存区
git commit -m <commit notes> # 将暂存区的内容提交到 HEAD
git commit -am <commit notes> # 将 add 和 commit 合并操作
git commit --amend -m <commit notes> # 合并到上次 commit
查看信息
git status # 显示状态
git diff [HEAD] # 显示差异
git log # 显示日志
git show <commit> # 显示某个 commit 的详细内容
git blame <file> # 显示文件每行的 commit 信息
撤回操作
git restore <file> # 撤回工作区的修改
git restore --staged <file> # 将暂存区修改撤回至工作区
git reset [--mixed] <commit> # 回退版本,保留工作区修改
git reset --soft <commit> # 回退版本,保留工作区+暂存区修改
git reset --hard <commit> # 回退版本,丢弃工作区所有修改
git rm <file> # 文件从工作区和暂存区删除
git mv <file> # 文件移动/重命名
git clean -df # 删除工作区未跟踪文件
分支、标签、暂存
git branch [--list] # 显示所有本地分支
git branch -a # 显示所有分支(含远程)
git branch <branch> # 创建分支
git branch -d|-D <branch> # 删除分支(-d安全删除 / -D强制删除)
git branch -m <newbranch> # 重命名当前分支
git switch <branch> # 切换已有分支
git switch -c <branch> # 创建并切换分支
git merge <branch> # 将指定分支合并到当前分支
git merge --abort # 取消合并,恢复合并前状态
git merge dev -Xtheirs # 合并dev,冲突以dev分支为准
git tag <tagname> # 给当前分支打标签
git stash # 保存工作区改动
git stash apply # 恢复stash内容,不删除stash记录
git stash drop # 删除一条stash记录
git stash pop # 恢复stash并删除记录
远程仓库
git remote [-v] # 查看远程仓库
git remote show <origin> # 查看指定远程仓库详情
git remote add <origin> <url> # 添加远程仓库地址
git remote rm <origin> # 删除远程仓库
git remote rename <oldname> <newname> # 重命名远程仓库
git pull [<origin> <branch>] # 拉取远程代码合并到本地
git push [-u <origin> <master>] # 推送本地分支到远程
git push origin --delete <branch> # 删除远程分支
git fetch # 获取远程更新,不自动合并
帮助
git help <command> # 查看命令完整文档
git <command> -h # 快速查看命令简要参数
checkout(不推荐优先使用,职责混杂)
该命令职责不明确,不建议使用;推荐改用 switch / restore
git checkout <file> # 丢弃工作区的文件修改
git checkout -f # 强制丢弃工作区和暂存区修改
git checkout <branch> # 切换分支
git checkout -b <branch> # 创建并切换分支