在 Git 中重新调整远程分支的基础

发布于 2024-11-11 14:45:21 字数 1217 浏览 4 评论 0原文

我正在使用中间 Git 存储库来镜像远程 SVN 存储库,人们可以从中进行克隆和工作。中间存储库的主分支每晚都会从上游 SVN 重新建立基础,我们正在研究功能分支。例如:

remote:
  master

local:
  master
  feature

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

remote:
  master
  feature

local:
  master
  feature

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

remote:
  master
  feature

local:
  master
  feature -> origin/feature

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

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

使远程功能分支与远程主分支保持最新。然而,这种方法会导致 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 pull 可以解决问题,但会导致我想避免的合并提交。我担心该消息指出 feature -> feature 而不是 feature -> origin/feature 但这可能只是一个演示。

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

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 its master branch rebased nightly from the upstream SVN, and we are working on feature branches. For example:

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:

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 pull does the trick but causes a merge commit that I'd like to avoid. I'm concerned that the message states feature -> feature rather than feature -> origin/feature but this may just be a presentation thing.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜夜流光相皎洁 2024-11-18 14:45:21

这取决于该功能是由一个人使用还是其他人正在使用该功能。

如果只有你自己,你可以在变基后强制推送:

git push origin feature -f

但是,如果其他人正在处理它,你应该合并而不是从 master 变基。

git merge master
git push origin feature

这将确保您与合作伙伴拥有共同的历史。

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

这是我关于每个功能分支主题的文章。

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:

git push origin feature -f

However, if others are working on it, you should merge and not rebase off of 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.

你怎么敢 2024-11-18 14:45:21

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

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

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

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.

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.

生活了然无味 2024-11-18 14:45:21

由于您在新的 master 之上重新建立了 feature 基础,因此您的本地 feature 并不是 origin/feature 的快进代码>不再。因此,我认为,在这种情况下,通过执行 git push origin +feature 来覆盖快进检查是完全可以的。您也可以在配置中指定这一点。

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

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

Because you rebased feature on top of the new master, your local feature is not a fast-forward of origin/feature anymore. 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

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 master into feature instead of rebasing. The result will indeed be a fast-forward.

眼中杀气 2024-11-18 14:45:21

我不熟悉使用 GitBash,我在这里为那些使用 Windows 的开发人员使用 TortoiseGit(新手 GUI)添加答案:

强制使用本地分支重新建立远程分支TortoiseGit

就像上面提到的许多人一样,如果只使用您的“私有”存储库分支来执行此操作,这意味着没有人会因为您重新建立远程“开发”或“功能”分支而向您扔椅子您的本地副本。其他已经承诺并推动了一周(或更多)工作的开发人员将从分支的历史记录(日志)中删除。

无法恢复它们 - 当然,您的共同开发人员仍然会对他们的更改进行更改,因为他们的 PULL 很可能会抛出错误,因为他们将远远领先于分支当前历史记录或最新提交与他们的相比。好消息是 Git 是一个分布式版本控制系统。

因此,请小心使用此恢复远程分支的权限。特此警告您。 ;)

I am unfamiliar using GitBash and I am adding an answer using TortoiseGit (GUI for newbees) here for those devs using Windows:

Force re-basing remote branch with local branch using TortoiseGit

Like many mentioned above, do this if only with your "private" repo branch and that means no one will ever throw a chair at you for re-basing the remote "develop" or "feature" branch with your local copy. The other devs who've committed and pushed their week's worth (or more) of work will be ERASED from the branch's history (log).

There will be no way to recover them - of course your co-developers will still have their changes on theirs as their PULL will most likely throw an error as they will be way way ahead of the branch current history or latest commits vs. theirs. Good thing Git is a distributed versioning system.

So, be careful with this power to reverting a remote branch. You are hereby warned. ;)

樱娆 2024-11-18 14:45:21

如果您正在与其他人一起开发功能分支,那么他们可能会提交一些内容。从那时起,您不能简单地推送更改,因为它们与提交历史记录相冲突。你可以做的是调用 git pull ,这是一个 git fetch + git merge 命令。这将为合并创建另一个提交。

我会尝试避免历史记录中不必要的合并提交,并通过简单地调用 git pull --rebase 来保持 git 提交历史记录干净。这会将您的提交放在现有提交之后。

当然,您应该只添加本地分支的 git 历史记录,而不要更改远程分支的 git 历史记录。

If you are working on a feature branch with other people, then it will probably happen that they have committed something. From there on, you cannot simply push your changes, as they conflict with the commit history. What you could do, would be to call git pull which is a git fetch + git merge command. This would create another commit for the merging.

I would try to avoid unnecessary merge commits in your history and keep your git commit history clean by simply calling git pull --rebase. This will put your commits after the existing ones.

Of course, you should only add the git history of your local branch, never change the git history of the remote branch.

从﹋此江山别 2024-11-18 14:45:21

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文