我在我的Windows 7机器上使用git,推送到Server 2008机器上的共享文件夹.这在过去6个月里一直运作良好.但是,截至昨天,我再也无法推送到远程仓库了.每次我尝试,我得到以下内容:
$ git push
Counting objects: 39, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (23/23), done.
Writing objects: 100% (23/23), 8.42 KiB, done.
Total 23 (delta 15), reused 0 (delta 0)
Unpacking objects: 100% (23/23), done.
error: Couldn't set refs/heads/my-branch
To //my-server/Code/my-project.git
! [remote rejected] my-branch -> my-branch (failed to write)
error: failed to push some refs to '//my-server/Code/my-project.git
Run Code Online (Sandbox Code Playgroud)
谷歌搜索'未能推送一些参考'错误给出了关于没有先拉(我完全是最新的),没有正确的权限(我可以完全访问所有内容,并且可以创建/删除/编辑文件)的各种结果在远程仓库通过资源管理器).
然后我偶然发现了这篇博客文章http://henke.ws/post.cfm/error-failed-to-push-some-refs,其中提到您可能必须在远程存储库上运行一些清理命令.所以我在远程存储库上运行git gc:
$ git gc
Counting objects: 3960, done.
Compressing objects: 100% (948/948), done.
Writing objects: 100% (3960/3960), done.
Total 3960 (delta 2971), reused 3942 (delta 2964)
Run Code Online (Sandbox Code Playgroud)
瞧,我可以再推!
$ git push
Counting objects: 39, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (23/23), done.
Writing objects: 100% (23/23), 8.42 KiB, done.
Total 23 (delta 15), reused 0 (delta 0)
Unpacking objects: 100% (23/23), done.
To //my-server/Code/my-project.git
8153afd..1385d28 my-branch -> my-branch
Run Code Online (Sandbox Code Playgroud)
但问题是,每次我想要推送时,我现在必须在远程存储库上运行gc .如果我不这样做,我会再次收到"未能推送一些参考"的错误.
那么,为什么我的回购被破坏了呢?我该如何永久解决问题?
您似乎正在通过 Windows 网络共享进行推送,而不是使用 ssh 或 git 协议。这意味着您的本地计算机必须将文件写入网络共享,因此可能会受到与该服务器相关的任何锁定文件或权限问题的阻碍。
推送分支时,写入对象后 git 将通过写入文件 .git/refs/heads/my-branch 来更新分支的引用。您的客户端当前似乎无法写入该文件。
当您执行 gc 时,将从 refs 目录中的松散文件中收集引用并将其放入文本文件 .git/packed-refs 中,然后删除各个引用文件。
我的理论是,当引用作为服务器上的松散文件存在时,您的推送会失败,因为您无权覆盖现有文件,但您有创建新文件的权限。使用 gc 打包 refs 后,ref 文件不再存在,因此您可以再次推送一次。
我建议的修复方法是:
作为最后的手段,如果您发现自己陷入了 gc 选项,您可以尝试运行“git pack-refs -all”。这比每次用 gc 修复问题要快。