Git 速查
Pro Git book,这是一本关于如何使用 Git 的优秀图书
git 规范
分支命名规范
feature/task-template-refactor # 功能开发
bugfix/constructor-parameter-fix # 错误修复
hotfix/critical-security-patch # 紧急修复
release/v1.2.0 # 发布分支
refactor/optimize-database-queries #用于代码重构。即不改变外部行为,但优化内部结构、提高可读性或性能。
perf/ #(性能专项):专门用于提升性能(Performance)的优化。
optimization/ # 合较大范围的综合优化
提交信息模板
<type>(<scope>): <subject>
<body>
<footer>
# 例如:
feat(TaskTemplate): add comprehensive constructor options
- Add support for all metadata fields in constructor
- Improve type safety with better parameter organization
- Maintain backward compatibility
Closes #123
Git 常用命令
配置
| 命令 | 说明 |
|---|
git config --global user.name "Your Name" | 设置全局 Git 用户名 |
git config --global user.email "youremail@yourdomain.com" | 设置全局 Git 用户邮箱 |
git config --list | 列出所有 Git 配置信息 |
这些值保存在全局配置文件 ~/.gitconfig 中
基本操作
| 命令 | 说明 |
|---|
git init | 初始化一个新的 Git 仓库 |
git clone [url] | 克隆远程仓库 |
git status | 查看当前仓库状态 |
git add [file] | 添加文件到暂存区 |
git add . | 添加所有文件到暂存区 |
git reset [file] | 取消暂存 |
git commit -m "message" | 提交暂存区的文件 |
git push [remote] [branch] | 推送本地分支到远程仓库 |
git pull | 拉取远程仓库的更新并合并到本地 |
git pull --rebase | 拉取远程仓库的更新并在本地变基 |
git fetch | 从远程仓库获取更新但不合并 |
分支操作
| 命令 | 说明 |
|---|
git branch | 列出所有本地分支 |
git branch -r | 列出所有远程分支 |
git branch [branch-name] | 创建新分支 |
git checkout [branch-name] | 切换到指定分支 |
git checkout -b [branch-name] | 创建并切换到新分支 |
git merge [branch-name] | 合并指定分支到当前分支 |
git branch -d [branch-name] | 删除本地分支 |
git push origin --delete [branch-name] | 删除远程分支 |
过滤掉保护分支并删除其余所有本地分支
git branch | ForEach-Object { $_.Trim() } | Where-Object { $_ -notmatch '^\*' -and $_ -ne 'main' -and $_ -ne 'development' } | ForEach-Object { git branch -D $_ }
清理远程仓库
git branch -r | ForEach-Object { $_.Trim() } | Where-Object { $_ -match '^origin/' -and $_ -notmatch 'main$' -and $_ -notmatch 'development$' -and $_ -notmatch 'HEAD' } | ForEach-Object { $branch = $_ -replace 'origin/', ''; git push origin --delete $branch }
标签操作
| 命令 | 说明 |
|---|
git tag | 列出所有标签 |
git tag [tag-name] | 创建新标签 |
git tag -d [tag-name] | 删除本地标签 |
git push origin [tag-name] | 推送标签到远程仓库 |
git push origin --delete [tag-name] | 删除远程标签 |
查看历史
| 命令 | 说明 |
|---|
git log | 查看提交历史 |
git log --oneline | 查看简洁的提交历史 |
git log --graph | 查看图形化的提交历史 |
git diff | 查看工作区与暂存区的差异 |
git diff [branch-name] | 查看当前分支与指定分支的差异 |
撤销操作
| 命令 | 说明 | 使用场景 |
|---|
git reset [file] | 撤销暂存区的文件 | |
git reset --hard <commit-hash> | 重置工作区和暂存区到最后一次提交。 | 把工作区和暂存区的文件删除 |
git reset --soft <commit-hash> | 重置到指定提交,但保留工作区和暂存区 | |
git reset --mixed <commit-hash> | 重置到指定提交,保留工作区,清空暂存区 | |
git revert <commit-hash> | 撤销指定的提交(创建新提交来撤销指定提交) | 撤销上一次的提交 |
git rebase --abort | 取消变基操作 | |
git revert HEAD --no-edit | 撤销上次的提交,并恢复;修改完代码提交,发现要求是另一个,使用此命令再重新修改并提交 | |
远程仓库
| 命令 | 说明 |
|---|
git remote -v | 查看远程仓库信息 |
git remote add [name] [url] | 添加远程仓库 |
git remote remove [name] | 删除远程仓库 |
git push [remote] [branch] | 推送本地分支到远程仓库 |
git pull [remote] [branch] | 拉取远程仓库的更新并合并到本地 |
子模块操作
| 命令 | 说明 | 使用场景 |
|---|
git submodule add <url> [path] | 将外部 Git 仓库作为子模块添加到当前仓库 | 在主项目中引入独立的第三方库、公共组件,或者为博客引入外部主题 |
git submodule init | 初始化本地 .gitmodules 文件中的子模块配置 | 首次克隆包含子模块的项目后,准备拉取子模块代码的前置操作 |
git submodule update | 根据主仓库记录的 commit 节点,拉取并检出子模块代码 | 主仓库记录的子模块版本有更新,需要同步本地子模块代码以保持一致 |
git submodule update --init --recursive | 一键初始化并更新所有子模块(包括嵌套的子模块) | 最常用:克隆完带有子模块的项目后,一键拉取所有关联的子模块代码 |
git clone --recurse-submodules <url> | 克隆主仓库的同时,自动初始化并更新所有子模块 | 全新克隆带有复杂子模块依赖的项目,省去后续手动 init 和 update 的麻烦 |
git submodule status | 查看所有子模块的当前状态(如 commit hash、路径等) | 检查子模块是否处于正确的提交节点,确认有没有出现 detached HEAD 状态 |
git submodule sync | 同步 .gitmodules 文件中修改的 URL 到本地 .git/config | 当远程子模块的仓库地址(URL)发生变更时,更新本地的关联配置 |
git submodule deinit <path> | 卸载指定的子模块,清空其工作区目录 | 暂时不需要参与某个子模块的开发,或者在彻底删除子模块前进行环境清理 |
git submodule foreach '<command>' | 在所有子模块中遍历执行指定的 shell 命令 | 需要同时在所有子模块里执行操作(比如统一执行 git pull 或 git checkout main) |
版本信息