Is it possible to add a sub folder with a .git folder to a repo, without Git treating it like a submodule? I've tried different methods to ignore that .git folder, but nothing this far has worked.
I have tried in /.gitignore:
/vendor/**/.git/
..and in /vendor/.gitignore:
.git/
The .git folder I want to ignore is in /vendor/foo/bar/.
Have you tried wildcards?
You can do this by directly adding some (any) internal content first. Git detects submodules by encountering the
.git
entry when searching a newly-encountered directory, but if you give it an actual path to look for inside that directory it doesn't search.So
Your question intrigued me so I tried it:
As per your question,
/tmp/foo
is a Git repository. And with respect to/tmp/foo
,/tmp/foo/bar
is a subdirectory which itself has a.git
directory.As you can see, the parent
foo
repository totally ignored thebar/.git
subdirectory and I didn't even need a.gitignore
file.And there's no way Git treats a path containing a
.git
folder as a submodule without having it registered in the.gitmodules
file.You can use git hooks to achieve what you want. Thinking out of the box, you could create a pre-commit hook that renames the .git directory of your included project, eg. to ".git2", add all files in the latter project except the ".git2" directory, commit all, push it and finally use post-commit hook to rename ".git2" folder back to ".git" in your module.
1) Create pre-commit file under .git/hooks/ of your root repo with contents:
2) Create post-commit file under .git/hooks/ also with contents:
3) Change a file in your repo and finally:
My Original answer
While it is true
.git
is automatically ignored, the main repo will treat/vendor/foo/bar
as a gitlink (special entry in the main repo index), and not as regular files.A trick would simply to move
bar/.git
outside the main repo:vendor/foo/bar
would be treated as a regular sub-folder, to be added to the main repo if need be.git
command withGIT_DIR=/path/to/bar/.git git ...
For instance:
As in jthill answer, you can add any item from subdirectory with
.git
and it will be treated as subdirectory instead of sub-module.But if you already registered this subdirectory as sub-module that has
.git
you have to first remove that sub-module but leave it in your working tree -git rm --cached a/submodule
.More info on: How do I remove a submodule?
Then you can repeat steps from jthill answer.
From my research there is no way to stop treating
.git
folder as sub-module. So the best solution that I can think of right now is to havebash
or any similar script that you can run in any folder in console. It should find any subfolders that have.git
and attempt to add any file from that folder to the main.git
, so it would stop treating them as sub-modules.Source of code below: Bash script to automatically convert git submodules to regular files
Remember to always commit/backup changes before trying new scripts to prevent any work/data loss.