即使执行了 'git pull',Git 仍然给出“non-fast-forward updates”错误提示。

3

Github是我的项目的默认存储库(只是将“origin”重命名为“github”)。出现了某种情况,导致“git push”引发“非快进更新”错误,尽管“git push github master”可行。“git pull”和“git pull github master”都表明状态是最新的。如何确保Github上没有未合并的更改,并纠正非快进错误?

$ git status
# On branch master
nothing to commit (working directory clean)
$ git pull
Already up-to-date.
$ git pull github master
From github.com:MikeBlyth/mission_net
 * branch            master     -> FETCH_HEAD
Already up-to-date.
$ git push github master
Everything up-to-date
$ git push
To git@github.com:MikeBlyth/mission_net.git
 ! [rejected]        add_command -> add_command (non-fast-forward)
error: failed to push some refs to 'git@github.com:MikeBlyth/mission_net.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

我的git配置文件是

[core]
  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true
[remote "github"]
  url = git@github.com:MikeBlyth/mission_net.git
  fetch = +refs/heads/*:refs/remotes/github/*
[branch "master"]
  remote = github
  merge = refs/heads/master
[remote "heroku"]
  url = git@heroku.com:joslink.git
  fetch = +refs/heads/*:refs/remotes/heroku/*
  merge = refs/heads/master
[remote "heroku"]
url = git@heroku.com:joslink.git
fetch = +refs/heads/*:refs/remotes/heroku/*

我已经修改了我的初始答案。 - VonC
总结michas和VonC的答案,问题在于“git push”默认尝试推送所有分支,而我有一个分支(add_command)不同步。 - Mike Blyth
2个回答

2

'git push'的语法支持显式和简写版本。 显式版本git push github master适用于您。简写版本git push不适用。

如果您使用简写版本,您没有告诉git要使用哪个远程仓库以及将本地分支推送到哪个远程分支。因此,git必须猜测您的意思。

您可以通过设置远程和push.default配置来进行配置:

   push.default
       Defines the action git push should take if no refspec is given on
       the command line, no refspec is configured in the remote, and no
       refspec is implied by any of the options given on the command line.
       Possible values are:

       ·    nothing - do not push anything.

       ·    matching - push all matching branches. All branches having the
           same name in both ends are considered to be matching. This is
           the default.

       ·    upstream - push the current branch to its upstream branch.

       ·    tracking - deprecated synonym for upstream.

       ·    current - push the current branch to a branch of the same
           name.

查看 git branch -vv 命令,以确定当前分支正在跟踪哪个分支。然后检查 git config --get push.default 命令,以验证它是否符合您的预期。


2
解释可能与远程“github”使用的默认refspec有关:
+refs/heads/*:refs/remotes/github/*

一个简单的 git push 将会推送:

  • 到与主分支(这里是 "github")相关联的远程仓库,因为根据 git statusmaster 是当前分支
  • 所有其它分支(masteradd_command 分支)

add_command 是那个与 github 远程仓库不同步的分支。

git checkout add_command 
git pull github

那么一个 git push 就可以起作用了。

一个 git checkout master 命令就足以离开分离头状态,然后他可以继续进行拉取和推送。 - dunni
@dunni但是根据他的状态,他已经在主分支中了。此时,reset --hard是确保本地和远程“github”之间没有任何“非快进”状态的最可靠方法。 - VonC
@dunni,实际上,我不认为“分离头”是根本原因。我已经修改了我的答案。 - VonC
感谢您提供简明扼要的答案。我使用了您和Michas的方法(因为我不理解为什么一个简单的push会推送所有分支),来解决这个问题。 - Mike Blyth
这对我有用。我检出了分支(即使我已经在该分支上)。然后git pull origin [branchname]。 - eeejay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接