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

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.