Git分支策略咨询:开发分支互合并后向master分支推送的风险及正确操作流程
Great question—let’s break this down so you can avoid headaches and keep your repo history clean.
Will your current approach cause errors?
Short answer: Yes, almost certainly. Here’s why:
You and your colleague have been working independently for two months, so both branch1 and branch2 are way out of sync with master. If you merge branch2 into branch1, push that to master, then your colleague tries to push their original branch2 to master, Git will reject the push. Remote master will have commits (your merged changes from branch2) that your colleague’s local branch2 doesn’t have, creating a conflicting history. They’ll have to resolve messy, hard-to-debug conflicts locally before they can push.
The Correct Workflow to Merge Branches & Push to Master Smoothly
Follow these steps to keep things organized and conflict-free:
1. First, sync both dev branches with the latest master
Both you and your colleague need to do this first to minimize downstream conflicts:
Your steps:
# Switch to master and pull the latest updates git checkout master git pull origin master # Merge the up-to-date master into your branch1 git checkout branch1 git merge master # Resolve any merge conflicts here (use a tool like VS Code for clarity) # Once conflicts are fixed, commit the merge git add . git commit -m "Merge latest master into branch1"
Your colleague’s steps:
They’ll run the exact same commands, replacing branch1 with branch2.
2. Merge each other’s dev branches (if you need mutual code)
Now that both branches are aligned with master, you can safely merge each other’s work:
Your steps to get branch2 code into branch1:
git checkout branch1 # Pull your colleague's latest branch2 (or fetch then merge) git pull origin branch2 # Resolve any conflicts between branch1 and branch2 # Commit the merge once done git add . git commit -m "Merge branch2 into branch1"
Your colleague’s steps:
They’ll merge your updated branch1 into their branch2 using the same pattern.
3. Push to master in a sequential, conflict-free way
Don’t push directly from your dev branch to master without syncing first. Here’s the safe order:
- You merge
branch1into master first:git checkout master git merge branch1 # Double-check no conflicts (there shouldn't be if you synced earlier) git push origin master - Your colleague syncs master before merging their
branch2:# They pull the latest master you just pushed git checkout master git pull origin master # Merge master into their updated branch2 to catch any new conflicts git checkout branch2 git merge master # Now merge branch2 into master and push git checkout master git merge branch2 git push origin master
Bonus: Use Pull Requests (If Your Team Supports It)
For even more safety and collaboration, create pull requests (PRs) instead of merging locally:
- You open a PR from
branch1tomaster - Your team reviews it, you resolve any feedback/conflicts
- Merge the PR to
master - Your colleague then opens a PR from
branch2tomaster—Git will automatically show if there are conflicts with the updated master, which they can resolve before merging.
This keeps a clear audit trail and ensures everyone sees the changes before they hit master.
Let me know if you run into tricky merge conflicts or need more detail on any step!
内容的提问来源于stack exchange,提问作者KItis

