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

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


当前回答

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

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

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

git pull origin main

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

其他回答

你基本上有两个选择:

You merge. That is actually quite simple, and a perfectly local operation: git checkout b1 git merge master # repeat for b2 and b3 This leaves the history exactly as it happened: You forked from master, you made changes to all branches, and finally you incorporated the changes from master into all three branches. git can handle this situation really well, it is designed for merges happening in all directions, at the same time. You can trust it be able to get all threads together correctly. It simply does not care whether branch b1 merges master, or master merges b1, the merge commit looks all the same to git. The only difference is, which branch ends up pointing to this merge commit. You rebase. People with an SVN, or similar background find this more intuitive. The commands are analogue to the merge case: git checkout b1 git rebase master # repeat for b2 and b3 People like this approach because it retains a linear history in all branches. However, this linear history is a lie, and you should be aware that it is. Consider this commit graph: A --- B --- C --- D <-- master \ \-- E --- F --- G <-- b1 The merge results in the true history: A --- B --- C --- D <-- master \ \ \-- E --- F --- G +-- H <-- b1 The rebase, however, gives you this history: A --- B --- C --- D <-- master \ \-- E' --- F' --- G' <-- b1 The point is, that the commits E', F', and G' never truly existed, and have likely never been tested. They may not even compile. It is actually quite easy to create nonsensical commits via a rebase, especially when the changes in master are important to the development in b1. The consequence of this may be, that you can't distinguish which of the three commits E, F, and G actually introduced a regression, diminishing the value of git bisect. I am not saying that you shouldn't use git rebase. It has its uses. But whenever you do use it, you need to be aware of the fact that you are lying about history. And you should at least compile test the new commits.

对于这个问题有两种选择。

1) Git 变基

2)去合并

只有与上述两者不同的合并情况下,将有额外的历史提交

1) git结帐分支(b1,b2,b3)

2) git rebase origin/master(如果冲突可以通过git rebase -continue在本地解决)

3)去推

或者,git合并选项是类似的方式

1) git checkout“your_branch”(b1,b2,b3)

2)转到合并母版

3)去推

从master更新你的分支:

  git checkout master
  git pull
  git checkout your_branch
  git merge master

如果你想要恢复到上一次提交并删除日志历史记录

使用下面的命令,让我们假设你想要去到以前的提交,它有commititid SHA - 71e2e57458bde883a37b332035f784c6653ec509,你可以指向这个提交,它将不会显示任何日志消息在这个提交之后,所有的历史将被擦除。

git push origin +71e2e57458bde883a37b332035f784c6653ec509^:master

Git checkout master git拉 Git签出feature_branch Git重基master Git push -f

你需要做一个强有力的推动后,对主调基