使用rossjrw/pr-preview-action时遇'fatal: not in a git repository'错误求助
修复
rossjrw/pr-preview-action 中的 fatal: not in a git directory 错误 问题根源
rossjrw/pr-preview-action 内部依赖 Git 命令完成分支切换、预览文件提交等操作,但你的 Workflow 中没有拉取当前仓库的代码,导致工作目录并非 Git 仓库根目录,触发该致命错误。
解决方案
在 Workflow 开头添加 actions/checkout@v4 步骤,拉取仓库代码以初始化 Git 环境。
修改后的完整配置
deploy-preview: name: "Deploy PR Preview" if: github.event_name == 'pull_request' needs: [ build-web ] runs-on: ubuntu-22.04 permissions: contents: write pull-requests: write steps: - name: "Checkout repository" uses: actions/checkout@v4 with: fetch-depth: 0 # 拉取完整仓库历史,确保能访问到 gh-pages 分支 - name: "Download build artifacts" uses: actions/download-artifact@v4 with: name: "out" # Must match the artifact name from build-web path: "preview-build" - name: "Deploy PR Preview to GitHub Pages" uses: rossjrw/pr-preview-action@v1 with: source-dir: "preview-build" # Must match path from download-artifact preview-branch: "gh-pages"
补充说明
actions/checkout@v4会将工作目录切换为仓库根目录,让后续的 Git 命令能正常识别仓库环境。fetch-depth: 0参数确保拉取完整的仓库历史,避免因分支历史不完整导致无法访问gh-pages分支的问题。
内容的提问来源于stack exchange,提问作者Cornelius Roemer
相关产品推荐
相关产品推荐

