简体   繁体   English

从 master 更新 Git 分支

[英]Update Git branches from master

I'm new to Git, and now I'm in this situation:我是 Git 的新手,现在我处于这种情况:

  • I have four branches (master, b1, b2, and b3).我有四个分支(master、b1、b2 和 b3)。
  • After I worked on b1-b3, I realized I have something to change on branch master that should be in all other branches.在我从事 b1-b3 工作之后,我意识到我在分支 master 上需要更改一些应该在所有其他分支中的东西。
  • I changed what I needed in master and... here is my problem:我改变了我在master中需要的东西......这是我的问题:

How do I update all other branches with master branch code?如何使用master分支代码更新所有其他分支?

You have two options:您有两个选择:

The first is a merge, but this creates an extra commit for the merge.第一个是合并,但这会为合并创建一个额外的提交。

Checkout each branch:结帐每个分支:

git checkout b1

Then merge:然后合并:

git merge origin/master

Then push:然后推:

git push origin b1

Alternatively, you can do a rebase:或者,你可以做一个rebase:

git fetch
git rebase origin/master

You have basically two options:您基本上有两种选择:

  1. 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.这使历史完全保持原样:您从 master 分叉,对所有分支进行了更改,最后将 master 的更改合并到所有三个分支中。

    git can handle this situation really well, it is designed for merges happening in all directions, at the same time. git可以很好地处理这种情况,它专为同时在各个方向发生的合并而设计。 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.它根本不在乎分支b1合并master还是master合并b1 ,合并提交对 git 来说看起来都是一样的。 The only difference is, which branch ends up pointing to this merge commit.唯一的区别是,哪个分支最终指向这个合并提交。

  2. You rebase.你变基。 People with an SVN, or similar background find this more intuitive.具有 SVN 或类似背景的人发现这更直观。 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:但是,rebase 为您提供了以下历史记录:

     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.关键是,提交E'F'G'从未真正存在过,并且可能从未被测试过。 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 .通过 rebase 创建无意义的提交实际上很容易,尤其是当master中的更改对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 .这样做的结果可能是,您无法区分EFG三个提交中的哪一个实际上引入了回归,从而减少了git bisect的值。

    I am not saying that you shouldn't use git rebase .我并不是说你不应该使用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.你至少应该编译测试新的提交。

git rebase master is the proper way to do this. git rebase master是执行此操作的正确方法。 Merging would mean a commit would be created for the merge, while rebasing would not.合并意味着将为合并创建一个提交,而变基则不会。

If you've been working on a branch on-and-off, or lots has happened in other branches while you've been working on something, it's best to rebase your branch onto master.如果您一直在断断续续地处理一个分支,或者在您处理某事时在其他分支中发生了很多事情,最好将您的分支重新设置为 master。 This keeps the history tidy and makes things a lot easier to follow.这使历史保持整洁,并使事情更容易遵循。

git checkout master
git pull
git checkout local_branch_name
git rebase master
git push --force # force required if you've already pushed

Notes:注意事项:

  • Don't rebase branches that you've collaborated with others on.不要对你与其他人合作过的分支进行 rebase。
  • You should rebase on the branch to which you will be merging which may not always be master.您应该基于要合并的分支,该分支可能并不总是主分支。

There's a chapter on rebasing at http://git-scm.com/book/ch3-6.html , and loads of other resources out there on the web.http://git-scm.com/book/ch3-6.html上有一章是关于变基的,网上还有很多其他资源。

@cmaster made the best elaborated answer. @cmaster 做出了最详尽的回答。 In brief:简而言之:

git checkout master #
git pull # update local master from remote master
git checkout <your_branch>
git merge master # solve merge conflicts if you have`

You should not rewrite branch history instead keep them in actual state for future references.您不应该重写分支历史记录,而是将它们保持在实际状态以备将来参考。 While merging to master, it creates one extra commit but that is cheap.在合并到 master 时,它会创建一个额外的提交,但这很便宜。 Commits does not cost.提交不花钱。

To update other branches like (backup) with your master branch copy.使用您的主分支副本更新其他分支,例如(备份)。 You can do follow either way (rebase or merge)...您可以按照任何一种方式进行操作(变基或合并)...

  1. Do rebase (there won't be any extra commit made to the backup branch).执行 rebase (不会对备份分支进行任何额外的提交)。
  2. Merge branches (there will be an extra commit automatically to the backup branch).合并分支(会有一个额外的自动提交到备份分支)。

    Note : Rebase is nothing but establishing a new base (a new copy)注意:Rebase 只不过是建立一个新的基础(一个新的副本)

 git checkout backup git merge master git push

(Repeat for other branches if any like backup2 & etc..,) (如果有任何像backup2等,请重复其他分支,)

 git checkout backup git rebase master git push

(Repeat for other branches if any like backup2 & etc..,) (如果有任何像backup2等,请重复其他分支,)

您可以合并,也可以使用git cherry-pick跨分支应用单个提交。

  1. git checkout master git结帐大师
  2. git pull
  3. git checkout feature_branch git checkout feature_branch
  4. git rebase master git rebase 大师
  5. git push -f git push -f

You need to do a forceful push after rebasing against master你需要在对 master 进行 rebase 后做一个强推

to update your branch from the master:从主更新你的分支:

  git checkout master
  git pull
  git checkout your_branch
  git merge master

There are two approaches有两种方法

  1. You want to merge the master branch into your branch您想将主分支合并到您的分支中

    - git checkout master - git pull - git checkout your-feature-branch - git merge master //resolve conflicts if any and commit - git push

2: If you want to rebase your changes on top of main. 2:如果您想在 main.js 之上重新调整您的更改。

 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

For everyone who finds this thread searching for an easy-to-use and consistent solution to merge your current branch with the latest changes on master:对于发现此线程的每个人都在寻找易于使用且一致的解决方案以将当前分支与 master 上的最新更改合并:

You can add this to your shell configuration:您可以将此添加到您的 shell 配置中:

alias merge='currentBranch=$(git rev-parse --abbrev-ref HEAD) && git checkout master && git pull && git checkout $currentBranch && git merge master'

This alias works with 5 commands:此别名适用于 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

After adding the alias, you can simply use the command “merge” to “update” the branch you are currently working on.添加别名后,您可以简单地使用命令“合并”来“更新”您当前正在处理的分支。

Surprisingly the most common method I use isn't mentioned.令人惊讶的是,我使用的最常用的方法没有被提及。 It's quite common when working Trunk Based Development style where main constantly gets updated and one is working from a branch from it.在使用基于主干的开发风格时,这是很常见的,其中main不断更新并且一个人正在从它的分支工作。

Let's say main already has the updated code and that you are in branch b1 .假设main已经有更新的代码,并且您在分支b1中。 If that's not the case, you'll need git fetch .如果不是这种情况,您将需要git fetch

So, to update b1 with the changes made in main , you can simply pull the code using因此,要使用main中所做的更改更新b1 ,您可以简单地使用

git pull origin main

The same will have to be done in the other branches when you, or someone else, gets to them and wants to update as well.当您或其他人访问它们并想要更新时,必须在其他分支中进行相同的操作。

There are two options for this problem.这个问题有两种选择。

1) git rebase 1) git rebase

2) git merge 2) git 合并

Only diff with above both in case of merge, will have extra commit in history在合并的情况下,只有与上述两者的差异,才会在历史记录中进行额外的提交

1) git checkout branch(b1,b2,b3) 1) git checkout 分支(b1,b2,b3)

2) git rebase origin/master (In case of conflicts resolve locally by doing git rebase --continue) 2) git rebase origin/master (如果发生冲突,通过执行 git rebase --continue 在本地解决)

3) git push 3) git 推送

Alternatively, git merge option is similar fashion或者, git merge 选项是类似的方式

1) git checkout "your_branch"(b1,b2,b3) 1) git checkout "your_branch"(b1,b2,b3)

2) git merge master 2)git合并主

3) git push 3) git 推送

IN CASE IF YOU WANT TO REVET TO A LAST COMMIT AND REMOVE THE LOG HISTORY AS WELL以防万一,如果您想恢复到最后一次提交并删除日志历史记录

Use below command lets say you want to go to previous commit which has commitID SHA - 71e2e57458bde883a37b332035f784c6653ec509 the you can point to this commit it will not display any log message after this commit and all history will be erased after that.使用下面的命令假设您要转到具有 commitID SHA - 71e2e57458bde883a37b332035f784c6653ec509的上一个提交,您可以指向此提交,它在此提交后不会显示任何日志消息,之后所有历史记录都将被删除。

git push origin +71e2e57458bde883a37b332035f784c6653ec509^:master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM