Git
约 1957 字大约 7 分钟
2026-03-17
分支
main主干分支,要打 TAGhotfix/<name>热修复分支develop开发分支feature/<name>功能分支release/iterX发布分支
记录格式
格式
<type>(<scope>): <subject>
<body>
<footer>字段说明
Breaking Changes(重大变更):
type(必填)— 提交类型
| 类型 | 说明 |
|---|---|
feat | 新功能 |
fix | 修复 bug |
docs | 文档更新 |
style | 代码格式调整(不影响逻辑,如空格、分号) |
refactor | 重构(既非新功能也非修复) |
perf | 性能优化 |
test | 添加或修改测试 |
build | 构建系统或依赖变更(如 Maven、Docker) |
ci | CI 配置变更(如 GitHub Actions、Jenkins) |
chore | 杂项(如 .gitignore、脚本工具) |
revert | 回滚某次提交 |
scope(可选)— 影响范围- 指明本次修改的模块或组件,如:
user,auth,payment,config。 - 若影响多个或全局,可省略或写
core、global。
- 指明本次修改的模块或组件,如:
subject(必填)— 简短描述- 用 祈使句 描述做了什么,例如:“add login validation”,而非 “added” 或 “adds”。
- 示例:
fix
body(可选)— 详细说明- 解释 为什么改、如何改、影响范围。
- 使用
-或*列表,每行不超过 72 字符。
footer(可选)— 关联信息- 关闭 Issue:
Closes或Fixes - Breaking Changes(重大变更):
- 关闭 Issue:
BREAKING CHANGE: The old API /v1/user is removed.三、示例
✅ 正确示例 1:普通修复
fix(payment): handle null amount in refund
- Add null check before processing refund
- Log warning instead of throwing exception
Closes #789✅ 正确示例 2:新功能
feat(user): add email verification endpoint
- Implement POST /api/v1/verify-email
- Integrate with Redis for token storage
- Add unit tests with MockMvc
Closes #101✅ 正确示例 3:重大变更
refactor(config): migrate to Spring Boot 3 config model
BREAKING CHANGE: Old property 'app.db.url' is replaced by 'spring.datasource.url'.
Users must update their application.yml.
Closes #202常见问题
.gitignore不生效
在项目开发过程中个,一般都会添加 .gitignore 文件,规则很简单,但有时会发现,规则不生效。 原因是 .gitignore 只能忽略那些原来没有被 track 的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。
解决方法就是先把本地缓存删除(改变成未 track 状态),然后再提交。
想要 .gitignore 起作用,必须要在这些文件不在暂存区中才可以,.gitignore 文件只是忽略没有被 staged(cached) 文件, 对于已经被 staged 文件,加入 ignore 文件时一定要先从 staged 移除,才可以忽略。
git rm -r --cached .git add .git commit -m 'update .gitignore'配置
查看配置
git config --list设置全局提交者信息(见下一章节)
SSH 公钥生成
ssh-keygen -t rsa -C "Simple words"测试(github 为例子)
ssh -T git@github.co设置提交者信息
全局配置
# 设置全局用户名
git config --global user.name "Your Name"
# 设置全局邮箱
git config --global user.email "your.email@example.com"项目级别配置
# 进入项目目录
cd /path/to/your/project
# 设置项目级用户名和邮箱
git config user.name "Project Specific Name"
git config user.email "project.email@example.com"验证
# 查看全局配置
git config --global user.name
git config --global user.email
# 查看当前项目配置(会显示项目级覆盖后的实际生效值)
git config user.name
git config user.email
# 查看所有配置及来源
git config --list --show-origin配置优先级
Git 配置按以下优先级生效(后者覆盖前者):
- 系统级
(/etc/gitconfig)- 最低优先级 - 全局级
(~/.gitconfig) - 项目级
(.git/config)- 最高优先级
常用命令
配置操作
| 操作 | 命令 |
|---|---|
| 全局设置用户名 | git config --global user.name "Your Name" |
| 全局设置邮箱 | git config --global user.email "email@example.com" |
| 项目级设置用户名 | git config user.name "Your Name" |
| 项目级设置邮箱 | git config user.email "email@example.com" |
| 查看全局配置 | git config --global --list |
| 查看项目配置 | git config --local --list |
| 查看所有配置 | git config --list --show-origin |
| 设置默认编辑器 | git config --global core.editor "vim" |
| 设置默认分支名 | git config --global init.defaultBranch main |
仓库操作
| 操作 | 命令 |
|---|---|
| 初始化新仓库 | git init |
| 克隆远程仓库 | git clone <url> |
| 克隆指定分支 | git clone -b <branch> <url> |
| 克隆到指定目录 | git clone <url> <directory> |
| 查看仓库状态 | git status |
| 查看简洁状态 | git status -s |
文件操作
| 操作 | 命令 |
|---|---|
| 添加文件到暂存区 | git add <file> |
| 添加所有修改 | git add . |
| 添加所有修改(含删除) | git add -A |
| 取消暂存文件 | git restore --staged <file> |
| 丢弃文件修改 | git restore <file> |
| 删除文件并暂存 | git rm <file> |
| 重命名文件 | git mv <old> <new> |
| 查看文件差异(工作区) | git diff |
| 查看文件差异(暂存区) | git diff --staged |
提交操作
| 操作 | 命令 |
|---|---|
| 提交暂存区 | git commit -m "message" |
| 提交并添加所有修改 | git commit -am "message" |
| 修改最后一次提交 | git commit --amend |
| 修改提交信息(未推送) | git commit --amend -m "new message" |
| 查看提交历史 | git log |
| 简洁历史(一行) | git log --oneline |
| 图形化历史 | git log --graph --oneline --all |
| 查看指定文件历史 | git log -p <file> |
分支操作
| 操作 | 命令 |
|---|---|
| 查看本地分支 | git branch |
| 查看远程分支 | git branch -r |
| 查看所有分支 | 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 merge <branch-name> |
| 变基分支 | git rebase <branch-name> |
远程仓库操作
| 操作 | 命令 |
|---|---|
| 查看远程仓库 | git remote -v |
| 添加远程仓库 | git remote add <name> <url> |
| 重命名远程仓库 | git remote rename <old> <new> |
| 删除远程仓库 | git remote remove <name> |
| 修改远程URL | git remote set-url <name> <url> |
| 获取远程分支(不合并) | git fetch <remote> |
| 拉取并合并 | git pull <remote> <branch> |
| 推送到远程 | git push <remote> <branch> |
| 强制推送 | git push -f <remote> <branch> |
| 推送所有分支 | git push --all <remote> |
| 推送标签 | git push <remote> --tags |
| 删除远程分支 | git push <remote> --delete <branch> |
标签操作
| 操作 | 命令 |
|---|---|
| 查看所有标签 | git tag |
| 创建轻量标签 | git tag <tag-name> |
| 创建附注标签 | git tag -a <tag-name> -m "message" |
| 删除本地标签 | git tag -d <tag-name> |
| 推送标签到远程 | git push origin <tag-name> |
| 推送所有标签 | git push origin --tags |
| 删除远程标签 | git push origin --delete tag <tag-name> |
| 检出标签 | git checkout <tag-name> |
撤销操作
| 操作 | 命令 |
|---|---|
| 撤销工作区修改 | git restore <file> |
| 撤销暂存区(保留修改) | git restore --staged <file> |
| 撤销最后一次提交(保留修改) | git reset --soft HEAD~1 |
| 撤销最后一次提交(丢弃修改) | git reset --hard HEAD~1 |
| 回退到指定版本 | git reset --hard <commit-id> |
| 查看所有操作记录 | git reflog |
| 撤销指定提交(新建提交) | git revert <commit-id> |
储藏操作
| 操作 | 命令 |
|---|---|
| 储藏当前修改 | git stash |
| 储藏并添加说明 | git stash push -m "description" |
| 查看储藏列表 | git stash list |
| 应用最近储藏(不删除) | git stash apply |
| 应用指定储藏 | git stash apply stash@{n} |
| 弹出最近储藏(删除) | git stash pop |
| 删除最近储藏 | git stash drop |
| 删除所有储藏 | git stash clear |
查看对比操作
| 操作 | 命令 |
|---|---|
| 查看工作区与暂存区差异 | git diff |
| 查看暂存区与仓库差异 | git diff --staged |
| 查看指定提交详情 | git show <commit-id> |
| 查看文件行级贡献者 | git blame <file> |
| 查看分支合并图 | git log --graph --decorate --oneline |
| 查看某文件修改历史 | git log -p <file> |
| 查看某行代码谁写的 | git blame -L <start>,<end> <file> |
子模块操作
| 操作 | 命令 |
|---|---|
| 添加子模块 | git submodule add <url> <path> |
| 初始化子模块 | git submodule init |
| 更新子模块 | git submodule update |
| 递归克隆含子模块 | git clone --recurse-submodules <url> |
| 更新并初始化子模块 | git submodule update --init --recursive |
清理操作
| 操作 | 命令 |
|---|---|
| 查看将被清理的文件 | git clean -n |
| 清理未跟踪文件 | git clean -f |
| 清理未跟踪文件和目录 | git clean -fd |
| 清理被忽略的文件 | git clean -fx |
| 垃圾回收 | git gc |
版权所有
版权归属:FelixJY
