Get "please make sure that the .gitmodules file is in the working tree" when running the git submodule add command

Viewed 14277

I'm new to git, currently looking at porting some large projects from mercurial. We have a root project that just contains the references to all the external projects (submodules in git). I'm trying to recreate this in git.

I have imported a project (foo) into githib. I've created a new empty project (root) and cloned it locally. I want to add Foo as a submodule using

git submodule add https://github.com/.../foo.git

from the /c/Work/GitHub/root (master)

but I keep getting "please make sure that the .gitmodules file is in the working tree".

Looking at the documentation, the first run of this command should create the .gitmodules file, but I get this error even if I create it by hand. Looking for this error on Google just returns the source files with the error but no explanation to why I'm getting it. I assume it's just my poor understand of git.

What I'm I doing wrong?

EDIT: I've also tried.

mkdir test
cd test
git init
git submodule add https://github.com/.../foo.git

I get the same error.

5 Answers

The check you're probably failing is:

int is_writing_gitmodules_ok(void)
{
        struct object_id oid;
        return file_exists(GITMODULES_FILE) ||
                (get_oid(GITMODULES_INDEX, &oid) < 0 && get_oid(GITMODULES_HEAD, &oid) < 0);
}

This means that either the file exists locally, or it doesn't exist in the staging area or the current HEAD commit.

You've used git add, but then deleted it from the working directory.

Use git restore .gitmodules (or similar) to bring the existing file back into your working directory.

You likely removed your .gitmodule or have changes to .gitmodule staged.

Try restoring the file using git restore .gitmodule.

If that doesn't help, try checking git status to ensure nothing is staged; otherwise run git reset .gitmodule.

Hard reset e.g. to the last commit or committing all the changes solved the problem for me.

I think the message should be in this case something like "commit your changes first" or so.

There was something wrong with my git installation. I uninstalled and reinstalled it the error went away.

Considering the current version of git-submodule.sh fails on:

 if ! git submodule--helper config --check-writeable >/dev/null 2>&1

If it possible you had a process keeping an handle on your .gitmodules, preventing any "git submodule add" command to modify it.

Note that this will slightly change With Git 2.33 (Q3 2021), with the rewrite of "git submodule"(man) in C, which does impact git submodule add:

See commit bbe3165 (23 Jul 2021) by Jeff King (peff).
See commit 8c8195e, commit a98b02c, commit 0008d12 (10 Jul 2021), and commit 84069fc (06 Jul 2021) by Atharva Raykar (tfidfwastaken).
(Merged by Junio C Hamano -- gitster -- in commit 10f57e0, 04 Aug 2021)

submodule: prefix die messages with 'fatal'

Signed-off-by: Atharva Raykar
Mentored-by: Christian Couder
Mentored-by: Shourya Shukla

The standard die() function that is used in C code prefixes all the messages passed to it with 'fatal: '.
This does not happen with the die used in 'git-submodule.sh'.

Let's prefix each of the shell die messages with 'fatal: ' so that when they are converted to C code, the error messages stay the same as before the conversion.

Note that the shell version of die exits with error code 1, while the C version exits with error code 128. In practice, this does not change any behaviour, as no functionality in 'submodule add' and 'submodule update' relies on the value of the exit code.

And:

submodule--helper: introduce add-clone subcommand

Signed-off-by: Atharva Raykar
Mentored-by: Christian Couder
Mentored-by: Shourya Shukla
Based-on-patch-by: Shourya Shukla
Based-on-patch-by: Prathamesh Chavan
Helped-by: Đoàn Trần Công Danh

Let's add a new "add-clone" subcommand to git submodule--helper with the goal of converting part of the shell code in git-submodule.sh related to git submodule add(man) into C code.
This new subcommand clones the repository that is to be added, and checks out to the appropriate branch.

This is meant to be a faithful conversion that leaves the behavior of 'cmd_add()' script unchanged.

Just in case anyone else has this issue. There was something wrong with my git installation. I uninstalled and reinstalled it the error went away.

Related