git failing to find the index when in submodule (“

2020-07-24 05:53发布

On a fresh git repo, after adding a submodule and changing to the submodule directory most git commands fail when run within the submodule with the error:

fatal: index file open failed: Not a directory

The full set of commands to reproduce the issue:

>  git init .
Initialized empty Git repository in /Users/drh/code/personal/Experiments/git-test/.git/
>  git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)
>  git submodule add git@github.com:mitsuhiko/flask.git thirdparty/flask
Cloning into 'thirdparty/flask'...
remote: Reusing existing pack: 9959, done.
remote: Total 9959 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (9959/9959), 5.17 MiB | 1.11 MiB/s, done.
Resolving deltas: 100% (5617/5617), done.
Checking connectivity... done.
>  cd thirdparty/flask
>  git status
fatal: index file open failed: Not a directory
>  cat .git
gitdir: ../../.git/modules/thirdparty/flask

git version 1.8.5.2 running on Mavericks

2条回答
Evening l夕情丶
2楼-- · 2020-07-24 06:19

That is because there isn't a commit yet in the parent repo (meaning no HEAD, and no master branch).
As you have detected, the GIT_DIR for the parent repo might be set too late for the submodule to use.
That would be a good bug to report if none of the git submodule tests cover that scenario.

Or, as the OP user3184153 commented:

I think that some other application (I suspecting virtualenv, though can't reproduce) was setting GIT_DIR.

查看更多
狗以群分
3楼-- · 2020-07-24 06:24

I personally was seeing this problem in my pre-commit hooks for some submodule automation. Environment variables GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE, etc.. were causing the issue. I was able to fix this problem with env -i git <git-command>, which is a simple way to use a clean bash enviornment with a single command.

查看更多
登录 后发表回答