以 feature/0.26 和 develop 分支为例
📋 目录
分支管理
查看分支
1 2 3 4 5 6 7 8
| git branch
git branch -a
git branch -r
|
创建分支
1 2 3 4 5 6 7 8 9 10 11
| git branch feature/0.26
git checkout -b feature/0.26
git checkout -b feature/0.26 develop
git checkout -b feature/0.26 origin/feature/0.26
|
切换分支
1 2 3 4 5 6 7 8 9
| git checkout develop
git checkout feature/0.26
git switch develop git switch feature/0.26
|
删除分支
1 2 3 4 5 6 7 8
| git branch -d feature/0.26
git branch -D feature/0.26
git push origin --delete feature/0.26
|
拉取与推送
拉取代码
1 2 3 4 5 6 7 8 9 10 11
| git pull origin develop
git pull origin feature/0.26
git fetch origin
git fetch origin feature/0.26
|
推送代码
1 2 3 4 5 6 7 8 9 10 11
| git push origin feature/0.26
git push -u origin feature/0.26
git push origin develop
git push -f origin feature/0.26
|
合并与变基
合并分支
1 2 3 4 5 6 7 8 9
| git checkout develop git merge feature/0.26
git merge --no-ff feature/0.26
git merge feature/0.26 -m "Merge feature/0.26 into develop"
|
变基操作
1 2 3 4 5 6 7 8 9 10 11 12
| git checkout feature/0.26 git rebase develop
git rebase -i develop
git rebase --continue
git rebase --abort
|
解决冲突
1 2 3 4 5 6 7 8 9 10 11
| git status
git add <冲突文件>
git commit
git rebase --continue
|
提交管理
提交代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git add .
git add src/index.js
git commit -m "feat: 完成 feature/0.26 功能开发"
git commit -am "fix: 修复 develop 分支的 bug"
git commit --amend
|
查看提交历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git log
git log --oneline
git log --graph --oneline --all
git log develop..feature/0.26
git log -- src/index.js
|
Cherry-pick
1 2 3 4 5 6
| git cherry-pick <commit-hash>
git checkout develop git cherry-pick abc123
|
状态查看
查看状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git status
git status -s
git diff
git diff --staged
git diff develop feature/0.26
|
查看远程信息
1 2 3 4 5 6 7 8
| git remote -v
git remote show origin
git branch -vv
|
撤销操作
撤销工作区更改
1 2 3 4 5 6 7 8 9
| git checkout -- src/index.js
git checkout -- .
git restore src/index.js git restore .
|
撤销暂存区
1 2 3 4 5 6 7 8
| git reset HEAD src/index.js
git reset HEAD
git restore --staged src/index.js
|
撤销提交
1 2 3 4 5 6 7 8 9 10 11
| git reset --soft HEAD~1
git reset --hard HEAD~1
git reset --hard abc123
git revert HEAD
|
标签管理
创建标签
1 2 3 4 5 6 7 8
| git tag v0.26
git tag -a v0.26 -m "Release version 0.26"
git tag -a v0.26 abc123 -m "Release version 0.26"
|
查看和推送标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git tag
git show v0.26
git push origin v0.26
git push origin --tags
git tag -d v0.26
git push origin --delete v0.26
|
远程仓库
同步远程分支
1 2 3 4 5 6 7 8 9 10 11
| git fetch origin
git fetch --all
git branch -r
git fetch --prune
|
跟踪远程分支
1 2 3 4 5 6 7 8
| git branch --set-upstream-to=origin/feature/0.26 feature/0.26
git branch -u origin/feature/0.26
git push -u origin feature/0.26
|
💡 常用工作流示例
场景1:开始新功能开发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git checkout develop
git pull origin develop
git checkout -b feature/0.26
git add . git commit -m "feat: 实现 0.26 新功能"
git push -u origin feature/0.26
|
场景2:合并功能到 develop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git checkout feature/0.26 git pull origin feature/0.26
git checkout develop git pull origin develop
git merge --no-ff feature/0.26
git push origin develop
git branch -d feature/0.26 git push origin --delete feature/0.26
|
场景3:同步 develop 的更新到 feature 分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git checkout feature/0.26
git fetch origin develop
git rebase origin/develop
git merge origin/develop
git push -f origin feature/0.26
|
场景4:紧急修复
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git checkout develop git checkout -b hotfix/urgent-fix
git add . git commit -m "fix: 紧急修复关键问题"
git checkout develop git merge hotfix/urgent-fix git push origin develop
git checkout feature/0.26 git merge hotfix/urgent-fix git push origin feature/0.26
|
⚠️ 注意事项
- 强制推送要谨慎:使用
git push -f 前确保不会影响其他人
- 合并前先更新:合并分支前确保两个分支都是最新的
- 提交信息要规范:使用清晰的提交信息,建议遵循约定式提交
- 定期同步 develop:在功能分支开发时定期同步 develop 的更新
- 避免直接在 develop 开发:始终通过功能分支进行开发
📚 Git 提交信息规范
1 2 3 4 5 6 7
| feat: 新功能 fix: 修复 bug docs: 文档更新 style: 代码格式调整 refactor: 重构代码 test: 测试相关 chore: 构建或辅助工具的变动
|
示例:
1 2 3
| git commit -m "feat: 添加用户登录功能" git commit -m "fix: 修复 feature/0.26 的空指针异常" git commit -m "docs: 更新 develop 分支的 README"
|