【问题标题】:Git gives 'non-fast-forward updates' error even after 'git pull'即使在“git pull”之后,Git 也会出现“非快进更新”错误
【发布时间】:2012-12-26 20:18:23
【问题描述】:

Github 是我项目的默认存储库(只是将“origin”重命名为“github”)。发生了一些事情,以至于“git push”导致“非快进更新”错误,即使“git push github master”有效。 “git pull”和“git pull github master”都表示最新状态。我如何 (a) 确保 Github 上没有未合并的更改并 (b) 更正非快进错误?

$ 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/*

【问题讨论】:

  • 我已经改变了我最初的答案。
  • 总结 michas 和 VonC 的答案,问题是“git push”默认会尝试推送所有分支,而我有一个不同步的分支(add_command)。跨度>

标签: git git-push fast-forward


【解决方案1】:

“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”的默认参考规范有关:

    +refs/heads/*:refs/remotes/github/*
    

    一个简单的git push 会推送:

    • 到与master关联的远程(这里是“github”),因为master是当前分支(根据git status
    • 所有其他分支(masteradd_command分支)

    add_command 是与github 远程不同步的那个。

    git checkout add_command 
    git pull github
    

    那么git push 就可以了。

    【讨论】:

    • 一个 git checkout master 是否足以离开分离的头部状态?然后他可以继续拉和推。
    • @dunni 但他已经是大师(根据他的状态)。此时,reset --hard 是确保本地和远程“github”之间不会出现任何“非快进”状态的最可靠方法。
    • @dunni 实际上,我不认为“分离的头”是根本原因。我改变了答案。
    • 感谢您的简洁回答。我用你的和 michas 的(因为我不明白为什么一个简单的推送会推动所有的分支)来解决这个问题。
    • 这对我有用。我对分支进行了结帐(即使我已经在分支上)。然后 git pull origin [branchname].
    猜你喜欢
    • 2021-12-22
    • 2011-05-17
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    相关资源
    最近更新 更多