代码之家  ›  专栏  ›  技术社区  ›  Jon

无法将提交两次推送到远程

  •  1
  • Jon  · 技术社区  · 6 年前

    我试图将修改后的承诺从一个回购推到另一个回购(在另一台机器上),并且很难理解为什么它第二次不工作。

    我的会话(已编辑,但仍相关):

    [/c/git/repo] (mybranch)
    [user] $ git commit -a --amend
    [mybranch ad1804290] Add filtering
     Date: Thu Jun 21 09:50:43 2018 -0400
     26 files changed, 2302 insertions(+), 2006 deletions(-)
    
    [user] $ git push centos7vm mybranch
    Counting objects: 157, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (135/135), done.
    Writing objects: 100% (157/157), 59.76 KiB | 3.51 MiB/s, done.
    Total 157 (delta 121), reused 34 (delta 15)
    To ssh://mymachine/home/user/git/repo
     * [new branch]          mybranch -> mybranch
    

    工作,对吧?让我们进行一些更改,然后重试:

    [/c/git/repo] (mybranch)
    [user] $ git commit -a --amend
    [mybranch 26c680cbf] Add filtering
     Date: Thu Jun 21 09:55:43 2018 -0400
     26 files changed, 2302 insertions(+), 2006 deletions(-)
    
    [/c/git/repo] (mybranch)
    [user] $ git push centos7vm mybranch
    To ssh://mymachine/home/user/git/repo
     ! [rejected]            mybranch -> mybranch (non-fast-forward)
    error: failed to push some refs to 'ssh://user@mymachine/home/user/git/repo'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    

    失败。。。但是,为什么?我甚至没有签出遥控器上的分支进行任何更改。我当前的分支如何位于远程服务器后面?这对我来说毫无意义。

    我的远程设置:

    [user] $ git remote show centos7vm
    * remote centos7vm
      Fetch URL: ssh://user@mymachine/home/user/git/repo
      Push  URL: ssh://user@mymachine/home/user/git/repo
      HEAD branch: development
      Remote branches:
      Local refs configured for 'git push':
        development        pushes to development        (fast-forwardable)
        mybranch           pushes to mybranch           (local out of date)
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Volodymyr Kononenko    6 年前

    当您第一次提交时,您的本地头从A更改为B。通过推到远程,您可以快速向前移动远程分支,使其头也指向B。

    当你修改时,你不是在B之后创建修订版C,你是在修改B,本质上是在修改B。最后,当您第二次尝试推送时,Git会验证B和B'具有相同的父级A,但具有不同的哈希,这是一个冲突。

        / B  (remote)
    A---
        \ B' (local)
    

    解决这个问题的一种方法是只使用-f标志覆盖远程提交,因为明显的原因,这是非常不鼓励的。

    听起来你的工作流程也有缺陷。理想情况下,您应该使用像gerrit或类似的代码审查系统,它允许您为同一提交推送后续的修改。只有通过审查、验证等的最后一次修订才在一个分支中结束。

        2
  •  0
  •   tmaj    6 年前

    如果你仔细观察你的命令,你会发现 --amend 实际上创建了一个新的提交。

    第一次尝试:

      [user] $ git commit -a --amend
      [mybranch ad1804290] Add filtering
    

    第二次尝试:

      [user] $ git commit -a --amend
      [mybranch 26c680cbf] Add filtering
    

    ( ad1804290 VS 26c680cbf )

    在推动后改变承诺很少是一个好主意。我只会让它保持原样。 如果需要更改,只需创建一个新的提交。

    因为你是自己工作的 push --force 这里可以,但是使用 -f 是个很不好的习惯。

    最好不要修改推送的承诺。