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.
The . gitmodules file, located in the top-level directory of a Git working tree, is a text file with a syntax matching the requirements of git-config[1]. The file contains one subsection per submodule, and the subsection value is the name of the submodule.
Git submodules allow you to keep a git repository as a subdirectory of another git repository. Git submodules are simply a reference to another repository at a particular snapshot in time. Git submodules enable a Git repository to incorporate and track version history of external code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With