如何避免重复消息“更新被拒绝,因为远程包含您所做的工作...”?

How to avoid repetitive message `Updates were rejected because the remote contains work that you do...`?

我正在与另外三个协作者一起参与一个项目,我的情况是:

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

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 的人让我担心。我不想破坏项目。

我怎样才能做到这一点?

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

每一个都独立工作时,您不会收到此消息。

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

您想将本地工作变基到远程分支。您可以通过将 --rebase 标志添加到 git pull:

来实现
git pull --rebase origin branch

或者你可以获取远程然后显式变基:

git fetch origin
git rebase origin/branch

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

还要记住rebase改写了历史!完成变基后,变基提交的提交 ID 将发生变化。