在 Git 中重新设置远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6199889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:03:43  来源:igfitidea点击:

Rebasing remote branches in Git

gitversion-controlbranchrebasefeature-branch

提问by kfb

I am using an intermediate Git repository to mirror a remote SVN repository, from which people can clone and work on. The intermediate repository has it's master branch rebased nightly from the upstream SVN, and we are working on feature branches. For example:

我正在使用一个中间 Git 存储库来镜像一个远程 SVN 存储库,人们可以从中克隆和工作。中间存储库的主分支每晚从上游 SVN 重新定位,我们正在研究功能分支。例如:

remote:
  master

local:
  master
  feature

I can successfully push my feature branch back to the remote, and end up with what I expect:

我可以成功地将我的功能分支推回远程,并最终得到我期望的结果:

remote:
  master
  feature

local:
  master
  feature

I then re-setup the branch to track the remote:

然后我重新设置分支以跟踪远程:

remote:
  master
  feature

local:
  master
  feature -> origin/feature

And all is well. What I would like to do from here is to rebase the feature branch to the master branch on the remote, but I would like to do this from my local machine. I'd like to be able to do:

一切都很好。我想从这里做的是将功能分支重新设置为远程的主分支,但我想从我的本地机器上执行此操作。我希望能够做到:

git checkout master
git pull
git checkout feature
git rebase master
git push origin feature

To keep the remote feature branch up-to-date with the remote master. However, this method causes Git to complain:

使远程功能分支与远程主分支保持同步。但是,这种方法会导致 Git 抱怨:

To <remote>
 ! [rejected]        feature -> feature (non-fast-forward)
error: failed to push some refs to '<remote>'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

git pulldoes the trick but causes a merge commit that I'd like to avoid. I'm concerned that the message states feature -> featurerather than feature -> origin/featurebut this may just be a presentation thing.

git pull可以解决问题,但会导致我想避免的合并提交。我担心消息是陈述feature -> feature而不是,feature -> origin/feature但这可能只是一个演示内容。

Am I missing something, or going about this in completely the wrong way? It's not critical to avoid doing the rebase on the remote server, but it makes fixing any merge conflicts from the rebase much harder.

我是否遗漏了什么,或者以完全错误的方式处理这个问题?避免在远程服务器上执行 rebase 并不重要,但它会使修复 rebase 中的任何合并冲突变得更加困难。

回答by Adam Dymitruk

It comes down to whether the feature is used by one person or if others are working off of it.

这归结为该功能是由一个人使用还是其他人正在使用它。

You can force the push after the rebase if it's just you:

如果只是您,您可以在 rebase 后强制推送:

git push origin feature -f

However, if others are working on it, you should merge and not rebase off of master.

但是,如果其他人正在处理它,您应该合并而不是从 master 上重新建立基础。

git merge master
git push origin feature

This will ensure that you have a common history with the people you are collaborating with.

这将确保您与正在合作的人有共同的历史。

On a different level, you should not be doing back-merges. What you are doing is polluting your feature branch's history with other commits that don't belong to the feature, making subsequent work with that branch more difficult - rebasing or not.

在另一个层面上,您不应该进行反向合并。您正在做的是使用不属于该功能的其他提交污染您的功能分支的历史记录,从而使该分支的后续工作更加困难 - 无论是否进行变基。

This is my article on the subject called branch per feature.

这是我关于名为branch per feature的主题的文章。

Hope this helps.

希望这可以帮助。

回答by ralphtheninja

Nice that you brought this subject up.

很高兴你提出这个话题。

This is an important thing/concept in git that a lof of git users would benefit from knowing. git rebase is a very powerful tool and enables you to squash commits together, remove commits etc. But as with any powerful tool, you basically need to know what you're doing or something might go really wrong.

这是 git 中一个重要的事情/概念,许多 git 用户将从中受益。git rebase 是一个非常强大的工具,它使您能够将提交压缩在一起,删除提交等。但是与任何强大的工具一样,您基本上需要知道自己在做什么,否则可能会出错。

When you are working locally and messing around with your local branches, you can do whatever you like as long as you haven't pushed the changes to the central repository. This means you can rewrite your own history, but not others history. By only messing around with your local stuff, nothing will have any impact on other repositories.

当您在本地工作并与本地分支打交道时,只要您没有将更改推送到中央存储库,您就可以做任何想做的事情。这意味着您可以改写自己的历史,但不能改写他人的历史。只搞乱你本地的东西,不会对其他存储库产生任何影响。

This is why it's important to remember that once you have pushed commits, you should not rebase them later on. The reason why this is important, is that other people might pull in your commits and base their work on your contributions to the code base, and if you later on decide to move that content from one place to another (rebase it) and push those changes, then other people will get problems and have to rebase their code. Now imagine you have 1000 developers :) It just causes a lot of unnecessary rework.

这就是为什么重要的是要记住,一旦你推送了提交,你以后就不应该重新设置它们。这很重要的原因是,其他人可能会拉入您的提交并根据您对代码库的贡献进行工作,如果您稍后决定将该内容从一个地方移动到另一个地方(重新定位它)并推送这些内容更改,然后其他人会遇到问题并且必须重新调整他们的代码。现在假设您有 1000 名开发人员 :) 这只会导致大量不必要的返工。

回答by Tilman Vogel

Because you rebased featureon top of the new master, your local featureis not a fast-forward of origin/featureanymore. So, I think, it's perfectly fine in this case to override the fast-forward check by doing git push origin +feature. You can also specify this in your config

因为你feature在新的基础上重新建立master,你的本地feature不再是快进origin/feature了。所以,我认为,在这种情况下,通过执行git push origin +feature. 你也可以在你的配置中指定这个

git config remote.origin.push +refs/heads/feature:refs/heads/feature

If other people work on top of origin/feature, they will be disturbed by this forced update. You can avoid that by merging in the new masterinto featureinstead of rebasing. The result will indeed be a fast-forward.

如果其他人在 之上工作origin/feature,他们会受到这种强制更新的干扰。您可以通过合并 new masterintofeature而不是 rebase来避免这种情况。结果确实会快进。

回答by Andrew Aylett

You can disable the check (if you're really sure you know what you're doing) by using the --forceoption to git push.

您可以通过使用--force选项来禁用检查(如果您真的确定自己知道自己在做什么)git push