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

未提交本地项目文件因Git操作丢失,求恢复方案

从babel-loader缓存恢复文件的操作步骤
  • 筛选目标缓存文件
    打开终端进入项目根目录,执行以下命令定位包含你丢失文件路径的缓存文件:

    grep -l "你的目标文件路径" ./node_modules/.cache/babel-loader/**/*.json
    

    命令执行后会列出所有匹配的JSON缓存文件,每个文件对应一个你曾编译过的模块。

  • 提取代码与还原原文件
    打开筛选出的JSON文件,重点关注两个字段:

    • code:Babel编译后的成品代码
    • map:对应的Source Map数据(若开启了source map编译选项则存在)
    1. 若存在map字段:把code内容保存为.js文件,将map内容保存为同名的.js.map文件,之后用source map反编译工具(本地可使用source-map npm包)还原出接近原代码的版本。
    2. 若无Source Map:直接提取code字段的内容,虽然这是转译后的代码(比如箭头函数转为普通函数、ES模块转为CommonJS),但核心逻辑完整,可在此基础上重构回原代码风格。
  • 批量恢复脚本(可选)
    若丢失文件较多,可使用以下Node.js脚本批量提取:

    const fs = require('fs');
    const path = require('path');
    
    const cacheDir = './node_modules/.cache/babel-loader';
    const outputDir = './recovered-files';
    
    fs.mkdirSync(outputDir, { recursive: true });
    
    function traverseDir(dir) {
      const files = fs.readdirSync(dir);
      files.forEach(file => {
        const filePath = path.join(dir, file);
        const stats = fs.statSync(filePath);
        if (stats.isDirectory()) {
          traverseDir(filePath);
        } else if (file.endsWith('.json')) {
          const content = JSON.parse(fs.readFileSync(filePath, 'utf8'));
          // 替换为你的项目路径关键词,比如src/components
          if (content.metadata?.filePath?.includes('src/')) {
            const originalFileName = path.basename(content.metadata.filePath);
            const outputPath = path.join(outputDir, originalFileName);
            fs.writeFileSync(outputPath, content.code);
            if (content.map) {
              fs.writeFileSync(`${outputPath}.map`, JSON.stringify(content.map));
            }
          }
        }
      });
    }
    
    traverseDir(cacheDir);
    

    将脚本保存为recover.js,替换代码中的src/为你的项目目录特征,执行node recover.js后,恢复的文件会存入recovered-files目录。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 23:30:18