Git Flow管理发布版与develop分支时,如何追溯应用热修复?
Great question! This is a common edge case when running Git Flow, and we can address it while staying true to your existing, well-running workflow’s core principles. Here’s a step-by-step breakdown that keeps everything consistent:
Step 1: Spin Up a Standard Hotfix Branch
Start by following Git Flow’s official hotfix initiation process to avoid breaking your workflow rhythm:
- Check out your
masterbranch and pull the latest production code:git checkout master git pull origin master - Create a descriptive hotfix branch (e.g.,
hotfix/urgent-production-fix):# Using the git-flow CLI tool git flow hotfix start urgent-production-fix # Manual branch creation if you don’t use the CLI git checkout -b hotfix/urgent-production-fix master
Step 2: Cherry-Pick the Critical Fix from Develop
First, grab the commit hash of the fix in your develop branch (use git log develop or your Git UI to find it—let’s say the hash is a1b2c3d):
- Cherry-pick the commit into your new hotfix branch:
git cherry-pick a1b2c3d - Critical: Resolve any merge conflicts that arise here. Since
mastermight lag behinddevelopin non-fix code, conflicts are possible—take time to test the fix after resolving them to ensure it works in the production context.
Step 3: Validate the Fix Thoroughly
Before merging, run all relevant tests, do a local sanity check, or deploy the hotfix branch to a staging environment if possible. Even if the fix worked in develop, the production codebase might have subtle differences—don’t skip this step.
Step 4: Finalize the Hotfix (Merge to Master & Develop)
Wrap things up using standard Git Flow hotfix procedures to keep your branches aligned:
If using the git-flow CLI:
git flow hotfix finish urgent-production-fixThis command automatically:
- Merges the hotfix into
master - Creates a version tag for the hotfix (e.g.,
v1.2.1) - Merges the hotfix back into
develop - Deletes the temporary hotfix branch
- Merges the hotfix into
If doing it manually:
- Merge to
masterand tag the release:git checkout master git merge --no-ff hotfix/urgent-production-fix git tag -a vX.Y.Z -m "Hotfix: Urgent fix ported from develop" - Merge back to
develop(if the original commit is already indevelop, Git might say "already up to date"—usegit merge --oursto add the merge commit without changing code):git checkout develop git merge --no-ff hotfix/urgent-production-fix # Use this if you get an "already up to date" message: # git merge --ours hotfix/urgent-production-fix - Delete the hotfix branch and push changes:
git branch -d hotfix/urgent-production-fix git push origin master develop --tags
- Merge to
Key Tips to Avoid Headaches
- Ensure the fix is self-contained: Make sure the cherry-picked commit doesn’t rely on other unmerged code in
develop. If it does, you’ll need to cherry-pick those dependent commits too (or re-evaluate if a full release is necessary instead). - Communicate with your team: Let everyone know you’re porting a fix from
developtomasterto avoid merge conflicts or duplicate work. - Document the hotfix: Add a clear message to the tag or merge commit so future contributors understand why this fix was ported separately.
内容的提问来源于stack exchange,提问作者S.Richmond

