You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将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.

  1. Prepare a clean working branch (never modify maintenance directly; 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-port
    
  2. Identify the commits to port:
    Run git log feature-branch to 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 from develop), first find the last common commit between develop and your feature branch:

    git merge-base develop feature-branch
    

    This gives you a hash like x9y8z7w—the base where your feature split from develop. The range of feature-only commits is x9y8z7w..feature-branch.

  3. 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 ., then git cherry-pick --continue to proceed. Use git cherry-pick --abort to cancel the process entirely if needed.

  4. Test thoroughly to ensure the feature works correctly on the maintenance branch (the codebase may differ from develop).

  5. Submit a PR from feature-maintenance-port to maintenance for 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.

  1. Create and prepare your working branch:

    git checkout maintenance
    git pull origin maintenance
    git checkout -b feature-maintenance-port
    
  2. Rebase feature commits onto maintenance:
    This command takes all commits on feature-branch that aren’t present on develop and applies them to maintenance:

    git rebase --onto maintenance develop feature-branch
    

    Resolve any conflicts as they appear, run git add ., then git rebase --continue. Use git rebase --abort to start over if needed.

  3. Test and submit a PR just like in Method 1.

Key Notes:

  • Never port commits that have already been merged into develop to maintenance—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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:07:10