简体   繁体   English

Git分支与大师分叉

[英]Git branch diverged from master

I had feature branch and then master branch . 我有feature分支,然后有主branch

Last week i worked on feature branch and then on feature branch i did this yesterday 上周,我在feature分支上工作,然后在功能分支上,我昨天做了这个

git rebase master . git rebase master

Other person aslo commited osme stuff in master branch today. 另一个人aslo今天在master分支中提交了osme的东西。

So today i did this 所以今天我做到了

master# git pull
master# git rebase feature

Now i get this 现在我明白了

$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 18 and 11 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean

What should i do. 我该怎么办。 can i do git push -f 我可以做git push -f

It would be extremely informative to try to create some diagrams explaining exactly what you did, so let's give that a shot. 尝试创建一些图表来确切说明您所做的工作将非常有用,因此让我们来看看。 Let's assume that you when you created your feature branch from the remote master, it had only 3 commits. 假设您从远程主机创建功能分支时,它只有3次提交。 This would mean the two local branches would start off looking like the following: 这意味着两个本地分支将开始如下所示:

A <- B <- C master
A <- B <- C feature

Let's also assume that during the last week, you made some commits to your local feature branch, and other developers on your team also made commits to the remote master. 我们还假设在上周,您对本地功能分支进行了一些提交,而您团队中的其他开发人员也对远程主机进行了提交。 After pulling your master branch via master# git pull , your two local branches would look like this: 通过master# git pull拉您的master分支后,您的两个本地分支将如下所示:

A <- B <- C <- 1 <- 2 <- 3 master
A <- B <- C <- D <- E feature

Here, the commits 1, 2, and 3 came from other developers, while commits D and E came from you. 这里,提交1、2和3来自其他开发人员,而提交D和E来自您。

Next, if you did a local rebase of master on the feature via master# git rebase feature , the two branches would look like this: 接下来,如果您通过master# git rebase feature在该功能上对master进行了本地重设,则两个分支将如下所示:

A <- B <- C <- D <- E <- 1' <- 2' <- 3' master
A <- B <- C <- D <- E feature

The master commits 1, 2 and 3 have now become 1', 2' and 3' because the are actually different commits made after replaying the feature on your master branch. 现在,主提交1、2和3已变成1',2'和3',因为实际上是在您的master分支上重播功能后所做的不同提交。

Finally, let's compare your local master branch with the remote master: 最后,让我们将本地master分支与远程master进行比较:

A <- B <- C <- D <- E <- 1' <- 2' <- 3' local master
A <- B <- C <- 1 <- 2 <- 3 remote master

When you did that git status call, Git told you that your local master has diverged from the remote. 当您进行git status呼叫时,Git告诉您您的本地主机已与远程主机分离。 What this means is that the local and remote master share a common ancestor commit, but each has different commits beyond that point. 这意味着本地和远程主节点共享一个共同的祖先提交,但在此之后,每个提交都有不同的提交。 This also means that you cannot fast forward the remote master. 这也意味着您无法快速转发远程主服务器。 Your only options at this point are to force push via git push -f , or to git merge . 此时,您唯一的选择是通过git push -f强制推送,或git merge As Oliver Charleworth pointed out, forcing your local branch to the remote is usually a bad idea because it can overwrite all the changes from other developers using master. 正如Oliver Charleworth指出的那样,将本地分支强制到远程通常是一个坏主意,因为它可以覆盖使用master的其他开发人员所做的所有更改。 Your remaining option is to merge your local master with the remote. 您剩下的选择是将本地主机与远程主机合并。

The easier (and cleaner) way to proceed moving forward is just to do all your work in the feature branch and then merge it into master when the time comes to do that. 前进的更简单(更简洁)的方法是在功能分支中完成所有工作,然后在需要时将其合并到master中。 Typically, you should only be rebasing a feature on (or merging a feature into) master, not the other way around. 通常,您仅应在主控上建立基础(或将功能合并到主控),​​而不是相反。

No, if you push -f, you will obliterate changes that were committed to master by the other developer. 不,如果您按-f键,则将消除其他开发人员提交给母版的更改。

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

1) Merge with master. 1)与主人合并。

git merge master

2) Rebase onto master 2)重新建立基础

git rebase <sha1> onto --master

<sha1> would be the original commit on master that you branched off from. <sha1>将是您分支出的master上的原始提交。

These options are very different from each other, and you need to fully understand what they do. 这些选项彼此非常不同,您需要完全了解它们的作用。 Which one you need to do depends entirely on what your intentions are, going forward. 您需要做什么,完全取决于您的意图,即前进。 If you explain exactly what you're trying to achieve, some further recommendations could be made. 如果您确切说明您要实现的目标,则可以提出一些进一步的建议。

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

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