我有四个分支(master, b1, b2和b3)。我在b1-b3上工作后,我意识到我在分支主上有一些应该在所有其他分支中更改的东西。我改变了我在master中需要的东西…这是我的问题:

我如何用主分支代码更新所有其他分支?


当前回答

Git rebase master是正确的方法。合并意味着将为合并创建一个提交,而重基则不会。

其他回答

有两种方法

您希望将主分支合并到您的分支中 - git checkout master - git拉 - git checkout your-feature-branch - git merge master //解决冲突并提交 - git push

2:如果你想在main的基础上重新调整你的改变。

 git checkout master #Switch to main branch
 git pull #Take latest
 git checkout your-feature-branch #Switch to story branch
 git pull --ff-only # Ensure branch is up to date
 git rebase -i origin master #Interactively rebase your commits on top of master. So your changes are on top of latest commits in main.
 git rebase --continue #Resolve conflicts and rebase --continue to continue with next commits
 git push -f origin your-feature-branch # As you have rewritten the commit history, you have to **force push** the commits

Git rebase master是正确的方法。合并意味着将为合并创建一个提交,而重基则不会。

您可以合并,也可以使用git的选择性跨分支应用单独的提交。

令人惊讶的是,我最常用的方法没有被提及。这在基于Trunk的开发风格中是很常见的,其中main不断更新,并且从它的分支中工作。

假设main已经有了更新的代码,并且你在分支b1中。如果不是这种情况,您将需要git fetch。

因此,要用main中所做的更改更新b1,只需使用

git pull origin main

当您或其他人到达其他分支并想要更新时,也必须在其他分支中进行相同的操作。

从master更新你的分支:

  git checkout master
  git pull
  git checkout your_branch
  git merge master