git pull上的Git错误(无法更新本地引用)

use*_*561 79 git command-line-interface

我只有分支主人,我每次尝试"git pull"时都会收到此错误:

error: Couldn't set refs/remotes/origin/master
From /var/lib/git/xxx/project
 ! a0f80ea..49177a3  master     -> origin/master  (unable to update local ref)
Run Code Online (Sandbox Code Playgroud)

当我做"git pull origin master"时,我得到:

error: Couldn't set ORIG_HEAD
fatal: Cannot update the ref 'ORIG_HEAD'.
Run Code Online (Sandbox Code Playgroud)

我一直在寻找,但无法找到原因

Ski*_*ack 173

我和我的团队遇到了这个错误,无法更新本地参考,在SourceTree中进行拉动.

我们用了 :

git gc --prune=now
Run Code Online (Sandbox Code Playgroud)

这将删除任何应该解决问题的重复引用对象.

以下是一些链接,您可以在其中了解有关git引用和修剪的更多信息:

一周的git小贴士

git-prune文档

git参考

  • 可能需要这两个命令:`git gc --prune=now` 来自 /sf/ask/209918271/ 的 `git Remote prune origin`更新本地参考 (8认同)
  • 也为我工作,同样的消息,Windows 7 上的 Sourcetree (2认同)
  • 刚开始也有这个问题。谢谢一堆!完美运行! (2认同)
  • @Skipjack 它确实暂时解决了问题。但是当我再次尝试获取并切换到新分支时,我遇到了同样的错误。 (2认同)

小智 68

我解决了如下:

git remote prune origin

  • 这为我做到了,`git gc --prune = now`没有做任何事情 (6认同)
  • 如果您添加有关其用途的评论,那就太好了。这样运行总是安全吗? (6认同)
  • 找到了!它确实有效.它与重命名的分支远程或其他东西有关.我不打算试着解释一下. (5认同)
  • 相同的。这对我有用。git gc --prune=now 对我不起作用。 (4认同)

小智 35

使用 gitbach line commande,用于git update-ref更新本地分支的引用:

$ git update-ref -d refs/remotes/origin/[locked branch name]
Run Code Online (Sandbox Code Playgroud)

然后拉使用 $ git pull

[locked branch name] 是由于提交 ID 不匹配而发生错误的分支的名称。


Abd*_*inu 21

直接回答

git remote prune origin
rm .git/refs/remotes/origin/master
git fetch
git pull origin master
Run Code Online (Sandbox Code Playgroud)

逐步运行上面的命令


eme*_*117 20

这对于 Windows 来说已经足够了:

git pack-refs --all

  • 这对我有用,谢谢。 (2认同)

cmt*_*kur 13

这对我来说非常有效:

rm -rf .git/packed-refs .git/rr-cache
Run Code Online (Sandbox Code Playgroud)


out*_*nds 10

我发现同样的错误消息试图从Bitbuck Repo拉入我的lokal副本.也只有一个Branche Master,命令git pull origin master会导致此错误消息

From https://bitbucket.org/xxx
 * branch            master     -> FETCH_HEAD
error: Couldn't set ORIG_HEAD
fatal: Cannot update the ref 'ORIG_HEAD'.
Run Code Online (Sandbox Code Playgroud)

解决方法如下

  1. git reflog 找到最后一次提交的编号
  2. git reset --hard <numnber> 重置为上次提交
  3. git pull origin master 再次拉,没有错误


the*_*ant 10

这里发生了什么事?对远程分支的本地引用已更改,因此当您运行时git pull,git 找不到任何相应的远程分支,因此失败。

git remote prune origin
Run Code Online (Sandbox Code Playgroud)

实际上清理了这个本地引用,然后git pull再次运行。

--dry-run建议 - 请选择安全选项运行


fkd*_*jsa 9

问题

Windows 用户经常会遇到这个问题

git pull 
Run Code Online (Sandbox Code Playgroud)

给出错误: error: cannot lock ref unable to update local ref

原因

原因 a)有多个分支,其 名称从开头到任何斜杠(或到结尾),仅大小写不同

Branch name clashing (upper/lower case)
#######################################

# Example 1)
#############################
feature/releasecandidate/fix123
feature/releaseCandidate/improveFeature789
------------------------
               ^
  Identical from beginning up to a slash (here the 2nd one)
  except for the marked letter, where the upper/lower case differs




# Example 2)
#############################
releaseBranch
releasebranch
-------------
       ^
  Identical from beginning to the end
  except for the marked letter
Run Code Online (Sandbox Code Playgroud)

原因 b)在 linux 上也是一个问题:一个分支是另一个分支的前缀,带有斜线边界

Prefix with slash-boundary
#######################################

# Example 1) - also a problem on linux
#############################
feature/release2021
feature/release2021/fixIssue07
                   ^
              slash boundary

# Example 2)
#############################
feature/release2022
feature/Release2022/fixIssue99
        ^          ^
  differing case   slash boundary
 (problem on 
   windows)
Run Code Online (Sandbox Code Playgroud)

解决方案

消除原因(请参阅上面的确切原因)。

git pull 
Run Code Online (Sandbox Code Playgroud)

例如:创建分支命名策略,例如全部为小写字母;或小写最后一个斜杠前的字母。或者一些智能钩子,检测违规。(但请注意:在原因 a)问题仅在 Windows 上,而不是在 linux 上)。

背景

问题是 windows 将这些分支(来自示例 1 和 2)存储在.git文件夹中

Branch name clashing (upper/lower case)
#######################################

# Example 1)
#############################
feature/releasecandidate/fix123
feature/releaseCandidate/improveFeature789
------------------------
               ^
  Identical from beginning up to a slash (here the 2nd one)
  except for the marked letter, where the upper/lower case differs




# Example 2)
#############################
releaseBranch
releasebranch
-------------
       ^
  Identical from beginning to the end
  except for the marked letter
Run Code Online (Sandbox Code Playgroud)

并且在原因 a)窗口无法区分大小写的差异,因此窗口上的 git 变得疯狂。

原因 b) 中,您不能拥有与文件 ( feature/release2021/) 同名的文件夹(例如feature/release2021)。

解决方法

通常有效的短期解决方法(直到您消除了原因)是:

Prefix with slash-boundary
#######################################

# Example 1) - also a problem on linux
#############################
feature/release2021
feature/release2021/fixIssue07
                   ^
              slash boundary

# Example 2)
#############################
feature/release2022
feature/Release2022/fixIssue99
        ^          ^
  differing case   slash boundary
 (problem on 
   windows)
Run Code Online (Sandbox Code Playgroud)


Xin*_*Xin 8

rm .git/refs/remotes/origin/master

它对我有用!

  • 如果您在分支上,请将“/master”更改为“/branch_name” (2认同)

小智 8

尝试在git仓库根文件夹中使用此命令:

rm .git/logs/refs/remotes/origin/master 
Run Code Online (Sandbox Code Playgroud)


tpg*_*114 7

确保正在执行的git pull用户与创建存储库的用户相同.文件权限不正确.


小智 6

对我有用的是:

git config --global fetch.prune true
Run Code Online (Sandbox Code Playgroud)

现在它继续自动运行修剪。


ros*_*osa 5

我已经删除了分支的本地引用:

git branch -d -r REPO/.git/refs/remotes/origin/BRANCHNAME
Run Code Online (Sandbox Code Playgroud)

然后我就可以进行抓取了。

或者更彻底

rm -rf REPO/.git/refs/remotes/origin
Run Code Online (Sandbox Code Playgroud)

这也每次都有效。