如何将feature branch的变更同步至历史不同的develop与maintenance分支?
Got it, this is a super common scenario when you have a long-lived maintenance branch that’s diverged from your main development line. The key here is to only bring over the specific commits from your feature branch without pulling in any unwanted code from develop. Here are two reliable ways to do this:
Method 1: Cherry-pick Specific Commits (Best for Granular Control)
This approach lets you pick exactly which commits from your feature branch to apply to maintenance—ideal if you only want parts of the feature or need to skip accidental commits.
Prepare a clean working branch (never modify
maintenancedirectly; use a feature branch for safety):# Switch to maintenance and pull the latest changes git checkout maintenance git pull origin maintenance # Create a new branch to hold your ported changes git checkout -b feature-maintenance-portIdentify the commits to port:
Rungit log feature-branchto view your feature’s commit history. Note the full hashes of the commits you want to move (e.g.,a1b2c3d,e4f5g6h). If you want all feature-specific commits (none fromdevelop), first find the last common commit betweendevelopand your feature branch:git merge-base develop feature-branchThis gives you a hash like
x9y8z7w—the base where your feature split fromdevelop. The range of feature-only commits isx9y8z7w..feature-branch.Cherry-pick the commits:
- For individual commits:
git cherry-pick a1b2c3d e4f5g6h - For a full range of feature commits:
git cherry-pick x9y8z7w..feature-branch
If merge conflicts occur, resolve them in your code editor, run
git add ., thengit cherry-pick --continueto proceed. Usegit cherry-pick --abortto cancel the process entirely if needed.- For individual commits:
Test thoroughly to ensure the feature works correctly on the maintenance branch (the codebase may differ from
develop).Submit a PR from
feature-maintenance-porttomaintenancefor review, then merge once approved.
Method 2: Rebase --onto (Faster for Full Feature Ports)
If you want to move all commits from your feature branch (and none from develop), git rebase --onto streamlines moving the entire feature’s history onto maintenance.
Create and prepare your working branch:
git checkout maintenance git pull origin maintenance git checkout -b feature-maintenance-portRebase feature commits onto maintenance:
This command takes all commits onfeature-branchthat aren’t present ondevelopand applies them tomaintenance:git rebase --onto maintenance develop feature-branchResolve any conflicts as they appear, run
git add ., thengit rebase --continue. Usegit rebase --abortto start over if needed.Test and submit a PR just like in Method 1.
Key Notes:
- Never port commits that have already been merged into
developtomaintenance—this creates duplicate commits that will cause future merge headaches. Stick strictly to your feature’s unique commits. - Always test the ported code thoroughly—maintenance branches often have older dependencies or different code, so the feature may need tweaks to work correctly.
内容的提问来源于stack exchange,提问作者mogagg

