除非我先在远程仓库上执行gc,否则git push会失败

时间:2012-09-07 16:19:37

标签: git

我在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

谷歌搜索'未能推送一些参考'错误给出了关于没有先拉(我完全是最新的),没有正确的权限(我有完全访问权限,并且可以创建/删除)的各种结果/通过资源管理器编辑远程仓库中的文件。

然后我偶然发现了这篇博文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)

瞧,我可以再推一次!

$ 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

问题是,我现在必须在远程存储库上运行gc 每次我想要推送。如果我不这样做,我会再次发现'未能推出一些参考错误'。

那么,为什么我的回购被破坏了呢?我该如何永久解决问题?

1 个答案:

答案 0 :(得分:4)

看起来你正在推动Windows网络共享,而不是使用ssh或git协议。这意味着您的本地计算机必须将文件写入网络共享,因此可能会受到与该服务器相关的任何锁定文件或权限问题的阻碍。

当推动分支时,在写入对象之后,git将通过写入文件.git / refs / heads / my-branch来更新分支的ref。看来该文件目前无法从您的客户端写入。

当您执行gc时,refs将从refs目录中的松散文件中收集并放入文本文件.git / packed-refs中,然后删除单个ref文件。

我的理论是,当ref作为服务器上的松散文件存在时,您的推送失败,因为您没有权限覆盖现有文件,而您有权创建新文件。用gc打包refs后,ref文件不再存在,因此你可以再次推送一次。

我建议的修复方法是:

  1. 验证是否可以覆盖客户端的现有文件,如果没有修复权限或文件锁定问题
  2. 切换到使用git协议,此博客似乎是对Windows http://freshbrewedcode.com/derekgreer/2012/02/19/hosting-a-git-repository-in-windows/的选项的一个很好的概述
  3. 作为最后的手段,如果你发现自己坚持使用gc选项,你可以尝试运行'git pack-refs -all'。它会比每次使用gc解决问题更快。