我有四个分支(master, b1, b2和b3)。我在b1-b3上工作后,我意识到我在分支主上有一些应该在所有其他分支中更改的东西。我改变了我在master中需要的东西…这是我的问题:
我如何用主分支代码更新所有其他分支?
我有四个分支(master, b1, b2和b3)。我在b1-b3上工作后,我意识到我在分支主上有一些应该在所有其他分支中更改的东西。我改变了我在master中需要的东西…这是我的问题:
我如何用主分支代码更新所有其他分支?
当前回答
对于每个发现这个线程的人来说,寻找一个易于使用和一致的解决方案来合并您当前的分支与master上的最新更改:
你可以把它添加到你的shell配置中:
alias merge='currentBranch=$(git rev-parse --abbrev-ref HEAD) && git checkout master && git pull && git checkout $currentBranch && git merge master'
这个别名使用5个命令:
currentBranch=$(git rev-parse --abbrev-ref HEAD) # gets your current branch(needed for point 4)
git checkout master # checks out master
git pull # gets latest changes from master
git checkout $currentBranch # checks out the in point 1 saved branch
git merge master # merges your current branch with master
在添加别名之后,您可以简单地使用“merge”命令来“更新”当前正在处理的分支。
其他回答
你基本上有两个选择:
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.
@cmaster给出了最好的详细回答。简而言之:
git checkout master #
git pull # update local master from remote master
git checkout <your_branch>
git merge master # solve merge conflicts if you have`
您不应该重写分支历史记录,而应该保持它们的实际状态以供将来引用。当合并到master时,它会创建一个额外的提交,但这很便宜。提交不需要成本。
用你的主分支副本更新其他分支,如(备份)。 你可以按照任何一种方式(rebase或merge)…
做rebase(不会有任何额外的提交到备份分支)。 合并分支(将有一个额外的自动提交到 备份分支)。 注:Rebase只是建立一个新的base(一个新的副本)
git 结帐备份 Git 合并大师 git push
(重复执行其他分支,如backup2等)
git 结帐备份 git 变基大师 git push
(重复执行其他分支,如backup2等)
对于每个发现这个线程的人来说,寻找一个易于使用和一致的解决方案来合并您当前的分支与master上的最新更改:
你可以把它添加到你的shell配置中:
alias merge='currentBranch=$(git rev-parse --abbrev-ref HEAD) && git checkout master && git pull && git checkout $currentBranch && git merge master'
这个别名使用5个命令:
currentBranch=$(git rev-parse --abbrev-ref HEAD) # gets your current branch(needed for point 4)
git checkout master # checks out master
git pull # gets latest changes from master
git checkout $currentBranch # checks out the in point 1 saved branch
git merge master # merges your current branch with master
在添加别名之后,您可以简单地使用“merge”命令来“更新”当前正在处理的分支。
从master更新你的分支:
git checkout master
git pull
git checkout your_branch
git merge master