You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.git/FETCH_HEAD文件始终为空的问题排查与解决请求

.git/FETCH_HEAD 始终为空的问题排查与修复

问题描述

未知原因导致.git/FETCH_HEAD文件始终为空,相关环境信息与操作日志如下:

$ uname -v   
Darwin Kernel Version 22.1.0: Sun Oct  9 20:14:54 PDT 2022; root:xnu-8792.41.9~2/RELEASE_X86_64
$ git --version
git version 2.37.1 (Apple Git-137.1)
$ git config --list
credential.helper=osxkeychain
init.defaultbranch=main
include.path=~/common/.gitconfig
core.excludesfile=/Users/khoan/.gitignore_global
user.name=Khoa Nguyen
user.email=khoa@nguyen.com
push.default=current
includeif.gitdir:~/srv/project/.path=~/srv/project/.gitconfig
user.email=khoa.nguyen@project.com
core.sshcommand=ssh -i ~/.ssh/project-id_ed25519
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=git@github.com:khoan/project.git
remote.origin.fetch=+refs/heads/main:refs/remotes/origin/main
$ git fetch origin feature-branch
Enter passphrase for key '/Users/khoan/.ssh/project-id_ed25519': 
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 7 (delta 4), reused 5 (delta 4), pack-reused 0
Unpacking objects: 100% (7/7), 2.90 KiB | 247.00 KiB/s, done.
From github.com:khoan/project
 * branch                    feature-branch -> FETCH_HEAD
$ wc .git/FETCH_HEAD
     0       0       0 .git/FETCH_HEAD

可能原因

  • 远程分支跟踪规则受限:当前remote.origin.fetch仅配置了跟踪main分支,部分旧版Git在直接fetch未跟踪分支时,可能出现FETCH_HEAD写入异常
  • .git目录权限不足:.git/FETCH_HEAD文件或其父目录的权限设置导致Git进程无法写入内容
  • Apple定制Git版本bug:你使用的git version 2.37.1 (Apple Git-137.1)存在特定场景下FETCH_HEAD写入失败的已知问题
  • 本地文件系统异常:磁盘文件系统错误导致文件写入后无法持久化保存

修复方案

  • 补全远程分支跟踪配置:执行以下命令让本地跟踪远程所有分支,之后重试fetch:
    git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
    git fetch origin feature-branch
    
  • 修复.git目录权限:检查并调整文件权限确保当前用户拥有读写权限:
    # 查看FETCH_HEAD权限
    ls -la .git/FETCH_HEAD
    # 修复权限
    chmod 644 .git/FETCH_HEAD
    # 若整个.git目录权限异常,可批量修复
    chmod -R u+rwX .git
    
  • 更新Git版本:卸载Apple自带Git,通过Homebrew安装官方版Git(若未安装Homebrew,可通过官方渠道安装):
    brew install git
    # 重启终端后验证版本
    git --version
    
  • 检查并修复文件系统:在macOS下执行磁盘验证与修复:
    # 验证磁盘状态
    diskutil verifyVolume /
    # 若存在错误,需重启进入恢复模式执行修复
    

内容的提问来源于stack exchange,提问作者Khoa Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 14:10:25