【Git】git revert 或 git stash 或 git restore 都无法正常恢复库文件

bash 复制代码
lijian@DESKTOP-7N7K2GN MINGW64 /lib/release(TEST_PROJECT)
$ git status
Refresh index: 100% (1567/1567), done.
On branch TEST_PROJECT
Your branch is behind 'origin/TEST_PROJECT' by 16 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   libwebrtc.a

lijian@DESKTOP-7N7K2GN MINGW64 /lib/release(TEST_PROJECT)
$ git restore libwebrtc.a
Encountered 1 file(s) that should have been pointers, but weren't:
        release/libwebrtc.a

根据你的 git statusgit restore 输出,问题出在 libwebrtc.a 是一个 Git LFS(Large File Storage)管理的文件,但当前未被正确识别为 LFS 指针文件,导致 Git 无法正常恢复它。


问题原因

  1. libwebrtc.a 应该由 Git LFS 管理

    • 该文件是二进制库(.a 文件),通常应该通过 Git LFS 存储(而不是直接提交到 Git)。
    • 但当前文件被误识别为普通 Git 文件,导致 git restore 失败。
  2. 错误信息分析

    bash 复制代码
    Encountered 1 file(s) that should have been pointers, but weren't:
            release/libwebrtc.a
    • Git 期望该文件是一个 LFS 指针文件 (如 version https://git-lfs.github.com/spec/v1 开头的文本),但实际是一个二进制文件。

解决方法

1. 检查 Git LFS 是否已安装并初始化

bash 复制代码
git lfs --version  # 确认 Git LFS 已安装

如果没有安装,请先安装 Git LFS:

  • Windows : 从 Git LFS 官网 下载安装。

  • Linux/macOS :

    bash 复制代码
    curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
    sudo apt-get install git-lfs  # Debian/Ubuntu
    brew install git-lfs          # macOS

然后初始化 LFS:

bash 复制代码
git lfs install

2. 确认 libwebrtc.a 是否在 .gitattributes 中被 LFS 管理

bash 复制代码
cat .gitattributes

如果 libwebrtc.a 未被正确标记,需添加规则:

bash 复制代码
echo "libwebrtc.a filter=lfs diff=lfs merge=lfs -text" >> .gitattributes
git add .gitattributes
git commit -m "Track libwebrtc.a with LFS"

3. 强制恢复 LFS 文件

由于文件状态异常,需用 git lfs pull 重新下载 LFS 文件:

bash 复制代码
git lfs pull

如果仍然失败,尝试:

bash 复制代码
git rm --cached libwebrtc.a       # 从 Git 缓存中移除
git reset --hard                  # 强制重置工作区
git lfs pull                      # 重新拉取 LFS 文件

4. 如果文件未正确提交到 LFS

如果该文件之前未正确通过 LFS 提交,需重新提交:

bash 复制代码
git rm --cached libwebrtc.a       # 移除错误提交的文件
git add libwebrtc.a               # 重新添加(此时会通过 LFS 处理)
git commit -m "Fix LFS tracking for libwebrtc.a"
git push origin TEST_PROJECT             # 推送修复后的提交

5. 检查分支状态

你的分支落后 origin/TEST_PROJECT 16 个提交,建议先更新:

bash 复制代码
git pull origin TEST_PROJECT

然后再尝试操作 LFS 文件。


总结

步骤 命令 说明
1. 安装 Git LFS git lfs install 确保 LFS 已初始化
2. 检查 .gitattributes cat .gitattributes 确认文件被 LFS 管理
3. 修复 LFS 跟踪 echo "libwebrtc.a filter=lfs diff=lfs merge=lfs -text" >> .gitattributes 添加 LFS 规则
4. 强制恢复文件 git lfs pullgit reset --hard 重新下载 LFS 文件
5. 重新提交文件 git rm --cached libwebrtc.a && git add libwebrtc.a 确保文件通过 LFS 提交

如果问题仍未解决,可能是仓库的 LFS 配置有误,建议联系仓库管理员确认 libwebrtc.a 是否应通过 LFS 管理。

相关推荐
旅者时光1 天前
Git使用基础
git
Clownorange1 天前
git安装和配置
git
网安2311 011 天前
OWASP ZAP 安全工具深度剖析:从环境搭建到架构复原的结对编程实践
git
ShineWinsu2 天前
对于Linux:git版本控制器和cgdb调试器的解析
linux·c语言·git·gitee·github·调试·cgdb
php_kevlin2 天前
git提交限制规范
大数据·git·elasticsearch
安大小万2 天前
Git 常用命令终极指南:从入门到进阶
git
摇滚侠2 天前
GIT 代码冲突 git pull 和 git pull rebase 的区别,保持提交记录的线性整齐
git
vistaup2 天前
windows git 更新当前目录下所有的文件(非递归)
windows·git
王码码20352 天前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
Irene19912 天前
Git 命令汇总表(基于一次完整的 Git 实战经验整理,涵盖从安装配置到日常开发、问题排查的所有常用命令)
git·常用命令