Git获取远程仓库数据失败:origin被拒绝

4

突然间我无法从远程获取数据了...

$git fetch origin
! [rejected]        master     -> origin/master  (non-fast-forward)

我不知道要寻找什么或发生了什么事情,因为它一直在工作到现在。关于配置方面并没有太多可说的。

[remote "origin"]
    url = http://xx.xxx.xx/project.git
    fetch = refs/tags/*:refs/tags/*
    fetch = refs/heads/*:refs/remotes/origin/*
    push = refs/tags/*:refs/tags/*
    push = refs/heads/master:refs/heads/master

其他同事可能在远程上做了些什么,但我们长期以来一直这样操作而没有遇到问题...


嗯,我执行了 $git fetch origin +refs/heads/master,似乎它起作用了...但我现在无法与远程合并:$ git merge origin master --> 致命错误:'origin' 没有指向一个提交 - Glasnhost
1个回答

5
  1. As you figured out by yourself, your fetch refspec is non-standard: the "+" prefix is missing. It is valid, just it's not what Git sets by default, as by default it overwrites the contents of remote branches when it fetches.

    The default refspec Git adds for a named remote looks like this:

    $ git config --get remote.origin.fetch
    +refs/heads/*:refs/remotes/origin/*
    

    So you should possibly just undo what you done by hand and stick with the defaults until you really understand how refspecs and fetching work (by reading the git-fetch manual page at least).

  2. git merge origin master means «merge the branches "origin" and "master" into the local branch» (doing the so-called "octopus merge").

    You either wanted git pull origin master (which means «fetch the branch "master" from "origin" and then merge it into the currently checked out branch») or git fetch origin followed by git merge origin/master, where "origin/master" refers to the remote branch supposedly created/updates as the result of git fetch origin doing its work (subject to the first point above).

底线是,似乎你试图通过编辑配置文件来创建一个远程,而不是只运行 git remote add <name> <git_url>。除非你真的了解内部工作原理,否则不要这样做。
同时,也需要 阅读有关远程分支的文章

好的!我没有编辑文件,它是从Eclipse创建的,为了有"+"前缀,"force update"选项也应该被选择,这不是默认的。合并origin master是一个打字错误,我不经常使用控制台...感谢你详细的解释! - Glasnhost
1
@Glasnhost,强制更新远程分支在获取时完全可以,因为它们存在的唯一原因是捕获(如果您愿意,可以缓存)您上次从该存储库获取时其自己的远程存储库的状态;换句话说,它们像书签一样工作,保存您每次想要了解其分支历史记录时不必访问该存储库。如果您需要“铸成石头”远程分支的特定状态,只需将您自己的本地分支分叉出来--并且git fetch不会对其进行干扰。因此,Eclipse在这里没有任何理由保持安全。 - kostix

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