首页 > 解决方案 > 无法将提交两次推送到我的遥控器

问题描述

我正在尝试将修改后的提交从一个存储库推送到另一个存储库(在另一台机器上),并且无法理解为什么它第二次无法正常工作。

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

[/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)

标签: gitgit-push

解决方案


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

第一次尝试:

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

第二次尝试:

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

ad180429026c680cbf

在推送之后更改提交很少是一个好主意。我会保持原样。如果您需要更改,只需创建一个新的提交。

由于您自己工作push --force可能在这里没问题,但使用-f是一个非常糟糕的习惯。

如果您不修改推送的提交,那就更好了。


推荐阅读