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

执行git add .报错'main/'未检出提交,上传GitHub失败求助

执行git add .时出现"error: 'main/' does not have a commit checked out"的原因及解决方法

我尝试通过以下步骤将本地文件夹上传为Git仓库:

cd "Your project directory"
git init
git add .
git commit -m "First commit"
git remote add origin "your GitHub URL"
git push -u origin main

但执行git add .后遇到错误:

error: 'main/' does not have a commit checked out
fatal: adding files failed

当前git status的输出为:

On branch main
Your branch is up to date with 'origin/main'.

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:   PPO_paper.py
    modified:   PPO_strappo.py
    modified:   strappo.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    __pycache__/
    gym_less_strappo.py
    less.py
    less_gym.py
    less_strappo.py
    logs/
    main/
    models/

no changes added to commit (use "git add" and/or "git commit -a")

问题原因

这个错误的核心原因是:main/目录本身是一个独立的Git仓库——它内部存在.git文件夹,Git会将其识别为嵌套仓库,默认不允许直接通过git add .将这类仓库的内容添加到父仓库中,因为这会破坏仓库的独立性和Git的管理逻辑。

解决方法

根据你的需求选择以下方案:

方案1:将main/作为普通目录纳入父仓库(不需要它独立)

如果main/不需要作为单独的Git仓库管理,直接删除它内部的Git配置即可:

# 进入main目录
cd main/
# 删除隐藏的.git文件夹(注意这会清除该目录的Git历史)
rm -rf .git
# 返回父仓库根目录
cd ..
# 重新执行添加操作
git add .

方案2:将main/作为子模块纳入父仓库(保留其独立仓库属性)

如果需要保留main/的独立Git仓库身份,可以通过Git子模块来管理:

# 先确保main/未被添加到暂存区(当前错误说明还没添加成功,可跳过此步)
git rm --cached main/
# 添加子模块,替换<main仓库的Git地址>为实际的仓库URL
git submodule add <main仓库的Git地址> main/
# 提交父仓库的子模块配置变更
git commit -m "Add main as submodule"

额外说明

从git status输出可以看到,你的本地仓库已经和远程origin/main关联且分支保持同步,说明之前已经完成过初始化和推送操作,本次是要新增文件和提交修改,只需解决main/目录的嵌套仓库问题即可继续执行后续步骤。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 09:45:44