【问题标题】:How to avoid repetitive message `Updates were rejected because the remote contains work that you do...`?如何避免重复消息`更新被拒绝,因为遥控器包含您所做的工作......`?
【发布时间】:2018-08-16 22:40:54
【问题描述】:

我正在与另外三个合作者一起工作,我的情况是:

每次我尝试添加一个新的提交并且远程有一些变化(即使它是一个我没有在本地工作过的文件)时,我都会收到以下消息,迫使我创建与以下内容的合并默认消息:

error: failed to push some refs to 'https://work.git.beanstalkapp.com/app.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

只有在遥控器没有变化的情况下才能避免这种情况。

这会导致提交历史记录中出现许多看起来像 Merge branch 'master' of https://work.git.beanstalkapp.com/app 的提交,我想避免这种情况。

我发现了一个相关的question,对于某些使用git push -f origin master 的人来说,它正在工作,但使用--force 让我担心。我不想破坏项目。

我怎样才能做到这一点?

【问题讨论】:

  • 是的,按照消息说的做,然后从远程拉取(或变基到)更改。
  • 这就是我正在做的,但这会创建看起来像 Merge branch 'master' of https://work.git.beanstalkapp.com/app 的提交,这是我想要避免的
  • 如果您将工作重新建立在远程工作之上,则不会。
  • 我更新了问题,以突出我想要避免的场景

标签: git git-pull


【解决方案1】:

您想对远程分支执行本地工作的变基。您可以通过将--rebase 标志添加到git pull 来做到这一点:

git pull --rebase origin branch

或者,您可以获取远程,然后显式地变基:

git fetch origin
git rebase origin/branch

请注意,这会使您在本地明确执行的任何合并变平。您可能需要添加 --preserve-merges / --rebase=preserve 以避免这种情况(阅读手册页了解详细说明)。

还请记住,rebase 会改写历史!在 rebase 完成后,rebase 提交的提交 ID 将发生变化。

【讨论】:

  • 是什么导致需要使用rebase?为什么我不能做一个简单的拉动?还是这是他的正常行为?我曾与 git 合作过,但以一种孤独的方式,这是我合作的第一个项目
【解决方案2】:

如果您处理真正不同的文件,将您的工作分开处理不同的 Git 分支是有意义的。

如果每个人都单独工作,您将不会收到此消息。

并且您可以在需要时将新分支合并到一个通用分支中。

【讨论】:

    猜你喜欢
    • 2018-03-29
    • 2020-08-07
    • 2022-01-08
    • 2013-08-22
    • 2016-11-08
    • 2021-07-30
    相关资源
    最近更新 更多