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

使用gh-pages部署React+TypeScript项目时遇Failed to get remote.origin.url错误求助

解决Create React App + TypeScript项目gh-pages部署失败问题

问题背景

用create-react-app创建了TypeScript React项目,尝试通过gh-pages部署,已配置origin远程仓库且仓库真实存在,按教程操作后仍部署失败。


详细信息

远程仓库配置

执行 git remote -v 输出:

origin  https://github.com/its-mrarsikk/tic-tac-toes.git (fetch)
origin  https://github.com/its-mrarsikk/tic-tac-toes.git (push)

初始package.json脚本

"scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

尝试添加-r参数后的脚本

"scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build -r https://github.com/its-mrarsikk/tic-tac-toes.git",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

部署错误输出

$ npm run deploy

> tictactoes@0.1.0 predeploy
> npm run build


> tictactoes@0.1.0 build
> react-scripts build

Creating an optimized production build...
Compiled successfully.

File sizes after gzip:

  46.57 kB  build/static/js/main.0cb0ace7.js
  1.78 kB   build/static/js/787.dafb6d84.chunk.js
  515 B     build/static/css/main.f855e6bc.css

The project was built assuming it is hosted at /tic-tac-toes/.
You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.


> tictactoes@0.1.0 deploy
> gh-pages -d build -r https://github.com/its-mrarsikk/tic-tac-toes.git

Error: Failed to get remote.origin.url (task must either be run in a git repository with a configured origin remote or must be configured with the "repo" option).
    at /run/media/marcel/Хранилище/Dev/Web/tictactoes/node_modules/gh-pages/lib/git.js:214:13
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

解决方案

1. 确认当前目录为Git仓库根目录

执行 git status,若提示不是Git仓库,说明你可能在子目录运行部署命令,切换到项目根目录后重试。

2. 重置远程仓库配置

即使git remote -v显示存在origin,也可能配置有异常,重新添加远程仓库:

git remote remove origin
git remote add origin https://github.com/its-mrarsikk/tic-tac-toes.git

3. 清理gh-pages缓存并强制部署

删除项目根目录下的.gh-pages文件夹(若存在),或直接用--clean参数清理后部署:

gh-pages -d build -r https://github.com/its-mrarsikk/tic-tac-toes.git --clean

4. 全局安装gh-pages重试

本地依赖的gh-pages可能存在问题,全局安装后直接运行部署命令:

npm install -g gh-pages
gh-pages -d build -r https://github.com/its-mrarsikk/tic-tac-toes.git

5. 检查Git权限配置

确保本地Git已配置正确的用户名和邮箱,且拥有仓库推送权限:

git config user.name "你的GitHub用户名"
git config user.email "你的GitHub绑定邮箱"

若用HTTPS方式,需确保能通过密码或个人访问令牌(PAT)完成认证。

6. 补充package.json的homepage字段

虽然当前错误和此字段无关,但为避免部署后页面空白,需添加正确的homepage:

"homepage": "https://its-mrarsikk.github.io/tic-tac-toes"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 00:23:15