# git-flow **Repository Path**: V2233/git-flow ## Basic Information - **Project Name**: git-flow - **Description**: git协作工作流测试 - **Primary Language**: JavaScript - **License**: GPL-3.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-11-18 - **Last Updated**: 2025-11-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # git-flow ```git```简要团队协作工作流 ## main/master单分支origin仓库步骤 ### 创建开发分支(仓库管理员) - 远程仓库创建后本地拉取 ```bash git clone https://gitee.com/ov0-lab/git-flow.git ``` - 从main或master创建本地dev分支 ```bash git checkout -b dev ``` - 创建远程dev分支 ```bash git push -u origin dev ``` 或 ```bash git push --set-upstream origin dev ``` ### 创建功能特性分支(团队成员) - 更新dev分支 ```bash git pull ``` - 从dev创建本地featureA分支 ```bash git checkout -b featureA/feature1 ``` - 创建远程dev分支 ```bash git push -u origin featureA/feature1 ``` ### 开发功能(团队成员) - 提交三部曲 ```bash git add . ``` ```bash git commit -m "feat(packages/featureA): 新增feature1" ``` ```bash git push ``` ### 合并featureA/feature1至dev分支 - 切换到dev分支 ```bash git checkout dev ``` - 更新本地dev分支 ```bash git pull ``` - 合并featureA/feature1分支 ```bash git merge --no-ff featureA/feature1 ``` - 推送dev至远程 ```bash git push ``` - 删除远程featureA/feature1分支(可选) ```bash git push origin --delete featureA/feature1 ``` - 删除本地featureA/feature1分支 ```bash git branch -d featureA/feature1 ``` ### 创建release分支 - 同理从dev创建本地release-0.1.0分支 ```bash git checkout -b release-0.1.0 ``` - 更新配置文件版本号(package.json)并提交推送 ```bash git add . ``` ```bash git commit -m "chore(package.json): bump version to 0.1.0" ``` ```bash git push -u origin release-0.1.0 ``` ### 合并release至主分支 - 切换到main分支 ```bash git checkout main ``` - 更新main ```bash git pull ``` - 合并release-0.1.0分支 ```bash git merge --no-ff release-0.1.0 ``` - 编辑tag ```bash git tag -a v0.1.0 main -m "Release v0.1.0: 新packages/featureA" ``` - 推送到远程 ```bash git push ``` - 推送tags ```bash git push --tags ``` - 在远程创建发行版 ### 主分支合并入dev分支 - 切换到dev分支 ```bash git checkout dev ``` - 更新dev分支 ```bash git pull ``` - 合并main分支 ```bash git merge --no-ff main ``` - 推送到远程 ```bash git push ``` - 删除远程release-0.1.0分支(可选) ```bash git push origin --delete release-0.1.0 ``` - 删除本地release-0.1.0分支 ```bash git branch -d release-0.1.0 ``` ## 生产bug快速修复 ### 从main创建hotfix分支并修复 - 切换到main分支 ```bash git checkout main ``` - 更新main ```bash git pull ``` - 创建本地hotfix-0.1.0分支 ```bash git checkout -b hotfix-0.1.0 ``` - 修复bug后提交三部曲 ```bash git add . ``` ```bash git commit -m "fix(packages/featureA): 修复feature1" ``` ```bash git push -u origin hotfix-0.1.0 ``` ### hotfix-0.1.0合并入main - 切换到main分支 ```bash git checkout main ``` - 更新main ```bash git pull ``` - 合并hotfix-0.1.0分支 ```bash git merge --no-ff hotfix-0.1.0 ``` - 推送到远程 ```bash git push ``` ### 给修复版本打tag - 编辑tag ```bash git tag -a v0.1.0-fix.1 main -m "Release v0.1.0-fix.1: 修复packages/featureA" ``` - 推送tags ```bash git push --tags ``` ### hotfix-0.1.0合并入dev - 切换到dev分支 ```bash git checkout dev ``` - 更新dev ```bash git pull ``` - 合并hotfix-0.1.0分支 ```bash git merge --no-ff hotfix-0.1.0 ``` - 推送到远程 ```bash git push ``` - 删除远程hotfix-0.1.0(可选) ```bash git push origin --delete hotfix-0.1.0 ``` - 删除本地hotfix-0.1.0分支 ```bash git branch -d hotfix-0.1.0 ``` ## 暂存更改 假设小明在featureA/feature1分支上开发并被要求紧急修复bug,此时featureA/feature1还未达到可以提交的进度,那么直接执行```git checkout dev```会丢失自己分支更改的代码,使用stash可以临时将更改的部分存到暂存区。 - 暂存更改操作 ```bash git stash ``` - 切到dev分支操作 ```bash git checkout dev ``` - 修完bug切回 ```bash git checkout featureA/feature1 ``` - stash内容应用到当前分支 ```bash git stash pop # 删除stash栈内容并放回该分支 ``` 或 ```bash git stash apply # 不会删除stash栈 ``` ```bash git stash drop # 后续删除 ``` ## cherry-pick 假设小明在dev分支上发现并修复了一个紧急bug(合并到了dev),现在,这个修复必须立刻应用到生产环境,但是dev分支现在不满足合并到主分支的条件,这时可以用cherry-pick将小明的更改操作提取出来应用到主分支上。 - 切换到主分支 ```bash git checkout main ``` - 更新本地代码 ```bash git pull ``` - 确保已更新dev分支,并用以下命令提取某次更改 ```bash git cherry-pick ``` ## 回退更改 首先查看当前git状态: ```bash git status ``` 会显示以下几种状态: | 状态 | 显示结果 | |---------------------------------------|------------------------------------------------------| | 改动代码但未执行 ```git add``` | modified: index.js | | 改动代码且已执行 ```git add``` | modified: index.js | | 已执行 ```git add 和 commit``` | nothing to commit, working tree clean | ### 有改动,无add,无commit - 查看改动文件 ```bash git status ``` 显示以下 modified: index.js - 撤回更改 ```bash git checkout -- index.js ``` ### 有改动,有add,无commit - 查看改动文件 ```bash git status ``` 显示以下 modified: index.js - 撤回更改 - 重置暂存区,保留工作目录更改 ```bash git reset HEAD --mixed index.js # 简写 git reset HEAD index.js ``` modified: index.js - 重置暂存区和工作目录,彻底丢弃更改 ```bash git reset HEAD --hard # 不推荐 ``` nothing to commit, working tree clean ### 有改动,有add,有commit - 保留:暂存区的更改、工作区的更改,仅重置提交历史 ```bash git reset HEAD~1 --soft ``` modified: index.js - 移动 HEAD 指针,重置暂存区,简写```git reset HEAD~1``` ```bash git reset HEAD~1 --mixed ``` modified: index.js - 丢弃:提交历史、暂存区、工作区的所有更改 ```bash git reset HEAD~1 --hard ``` nothing to commit, working tree clean ### 有改动,有add,有commit,且push - 使用revert ```bash git revert --no-edit ``` ```bash git push ``` ## 合并历史记录问题 使用```git merge```能保留完整的提交历史,当然它的非线性也带来了commit记录不美观问题,对于```git merge```和```git rebase```选择,推荐在共享分支(main/master、dev)上使用```git merge```,在个人或功能特性分支选择```git rebase``` 同样,对于等价于```git fetch```+```git merge```的```git pull```命令也可以替换成```git pull --rebase```来获得线性、简洁历史记录 ## 参考 git基本命令:[https://github.com/arslanbilal/git-cheat-sheet](https://github.com/arslanbilal/git-cheat-sheet) 综合资料:[https://github.com/xirong/my-git](https://github.com/xirong/my-git)