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

使用Gitflow Workflow时,master分支更新后如何同步分支代码?

同步上游master变更到你的Gitflow分支

嘿,这个场景在基于fork的Gitflow协作里太常见了,我来给你一步步拆解怎么操作:

1. 先确保能获取上游仓库的最新代码

首先你得确认本地仓库已经关联了原仓库(也就是你fork的那个仓库)作为上游远程:

  • 查看当前远程仓库列表:
    git remote -v
    
  • 如果没看到上游仓库的地址,就添加它:
    git remote add upstream <原仓库的Git克隆地址>
    
  • 拉取上游master分支的最新变更:
    git fetch upstream master
    

2. 更新你的本地develop分支

接下来把上游master的新提交同步到你的develop分支:

  • 切换到develop分支:
    git checkout develop
    
  • 合并上游master的最新代码到本地develop:
    git merge upstream/master
    

    提示:如果合并过程中出现冲突,直接在代码里解决冲突,然后用git add <冲突文件>标记冲突已解决,再执行git commit完成合并。

  • 把更新后的develop推送到你的fork仓库:
    git push origin develop
    

3. 同步变更到你的feature分支

最后把更新后的develop分支同步到每个feature分支:

  • 切换到其中一个feature分支(比如feature/user-profile):
    git checkout feature/user-profile
    
  • 合并本地develop的最新代码:
    git merge develop
    
    同样,有冲突就解决后提交。
  • 推送到你的fork仓库对应分支:
    git push origin feature/user-profile
    
  • 另一个feature分支重复以上三步操作即可。

可选:用Rebase整理提交历史

如果你偏好更干净的线性提交历史,也可以用rebase代替merge来同步develop的变更:

git checkout feature/user-profile
git rebase develop

注意:如果这个feature分支已经和其他人协作过,或者已经推送到远程仓库并被其他人拉取过,不要用rebase,否则会打乱协作历史。只有你自己单独使用的feature分支才适合用rebase。

内容的提问来源于stack exchange,提问作者Mattia

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:43:13