如何在testing分支开发时同步master分支的修复内容且不丢失testing分支进度?
Hey there, let's clear up the confusion first: merging master into your testing branch won't make you lose any of your ongoing development work—you just need to handle the process correctly. Let's walk through two reliable approaches, plus some best practices to keep things smooth.
Option 1: Merge Master into Testing (Safe & Team-Friendly)
This is the most straightforward and safe method, especially if you're collaborating with others. It preserves the full history of both branches and avoids rewriting commits.
- First, make sure your local
masterbranch is up-to-date with the remote:git checkout master git pull origin master - Switch back to your
testingbranch:git checkout testing - Merge the updated
masterintotesting:git merge master - If you hit merge conflicts (which happens when both branches changed the same parts of code), open the conflicting files, edit them to resolve the discrepancies, then stage and commit the fix:
git add <conflicted-file-name> git commit
The merge will create a new "merge commit" that ties the two branch histories together—none of your testing branch commits will be lost or overwritten.
Option 2: Rebase Testing onto Master (Cleaner Linear History)
If you prefer a cleaner, linear commit history (no merge commits), you can use rebase. Important note: Only use this if testing is a private branch (you're the only one working on it)—rebase rewrites commit history, which can cause chaos if others are relying on the remote testing branch.
- Ensure your local
masteris up-to-date:git checkout master git pull origin master git checkout testing - Rebase your
testingbranch onto the updatedmaster:git rebase master - If conflicts pop up during rebase, resolve them in the files, then:
Repeat until the rebase finishes.git add <conflicted-file-name> git rebase --continue
This will replay all your testing branch commits on top of the latest master commits, giving you a clean, linear history.
Best Practices to Avoid Headaches
- Sync regularly: Don't wait until
masterhas dozens of new commits to sync. Do it weekly or after finishing a small feature—fewer changes mean fewer conflicts to fix. - Backup first: If you're nervous about messing up, create a backup branch before merging/rebase:
You can always switch back to this backup if something goes wrong.git checkout testing git checkout -b testing-backup - Test after sync: Always run your project's tests after syncing to make sure the combined code works as expected before continuing development.
内容的提问来源于stack exchange,提问作者Alessio Conte

