【问题标题】:Failed to push some refs to remote repository无法将一些 refs 推送到远程存储库
【发布时间】:2014-07-07 06:05:52
【问题描述】:

我在我的局域网中创建了一个新的远程存储库并添加了项目(PROJECT 1PROJECT 2)。

用户 A 和用户 B 一次克隆存储库,用户 A 在 PROJECT 1 中进行更改并推送这些更改。

用户 B 在PROJECT 2 中进行了更改,他没有在PROJECT 1 中进行任何更改,并且在推送数据时他也只选择了PROJECT 2 内容,但推送时出错。

ERROR :

error: failed to push some refs to http://gitbub.com
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 merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.`

如何允许用户 2 的推送?

【问题讨论】:

  • 看起来PROJECT1PROJECT2 都需要自己的仓库。

标签: git


【解决方案1】:

正如jcm 所评论的那样,最简单的解决方案是将 project1 和 2 放在他们自己的 repos 中。

但是如果它们在同一个仓库中,那么:

User2 仍然需要在更新的 repo 之上重新调整他/她的工作。

git pull --rebase

与单独的 git pull 相比,这允许您在更新的远程跟踪分支之上重播本地提交。

x--x--x--x--y--y--y (master): y means Project2 commits
         |
  (origin/master)

git pull --rebase = git fetch + git rebase

git fetch 表示:

x--x--x--x'--x' (origin/master)
       \
        y--y--y

git rebase 表示:

x--x--x--x'--x'--y'--y'--y' (master)
             |
      (origin/master)

现在git push 将变得微不足道,将您的本地提交添加到原始主机之上。

由于 User2 的工作与 User1 的工作是分开的,因此 rebase 将在 origin/master 之上简单地重放 User2 的提交,并允许 (fast-forward) 推送。

由于两个项目在同一个repo中,如果在同一个分支上修改就会出现这种情况。

一个好的解决方法是创建两个分支,每个项目一个。

如果你需要这两个项目,那么你需要在推送之前pull --rebase
可以考虑making the rebase automatic on a git pull


单独的多个git pull 会引入此处不需要的合并提交(因为修改是在两组单独的文件上完成的)

x--x--x--x'----x' (origin/master)
       \        \
        y--y--y--m (master)

然后下拉:

x--x--x--x'----x'--x'---x' (origin/master)
       \        \        \
        y--y--y--m--y--y--m (master

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 2021-06-11
    • 2018-06-24
    • 2010-10-25
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多