It seems a git repo inside a parent repo isn't included in a commit on the parent unless it's setup as a submodule. Is it possible to override this behaviour and treat the nested git repo as any other directory? I don't want to rely on external dependencies through submodules but want to use git to manage these dependencies within the project.
- I am not sure if this is the right way to "mange dependencies", but I propose two ways to achieve what you want, plus some precisions about the nature of submodules in Git.VonC– VonC2010-02-23 12:04:21 +00:00Commented Feb 23, 2010 at 12:04
- 1It sounds like what you really want is a subtree merge. That will leave you with a single repository and a single history.user597474– user5974742011-08-25 20:09:26 +00:00Commented Aug 25, 2011 at 20:09
- Are you sure that a git repo inside a parent repo isn't included in a commit on the parent unless it's setup as a submodule? Because that's what I want, and if you're right, then Git has that out-of-the-box.Saeed Neamati– Saeed Neamati2016-07-13 05:04:51 +00:00Commented Jul 13, 2016 at 5:04
5 Answers
1/ You could override that through:
- either a git config setting: set the environment variable
$GIT_DIR
you define your.gitdirectory of the nested Git working tree as an external.git(external to both the nested repo and the main repo) - or by setting your nested repo 'N' outside the main repo, but checkout that repo 'N' inside the main repo:
core.worktree
Set the path to the root of the work tree. This can be overridden by the GIT_WORK_TREE environment variable and the --work-tree command line option
In both case, the idea is to have a nested worktree without any .git subdirectory in it.
2/ With submodules, the nested git repo is not really included in the parent repo.
A special entry is made in the parent tree to record the external Git SHA1.
new file mode 160000 index 0000000..4c4c5a2 See also "nature of Git submodules" (third part of the answer)
Nine years later, this discussion is quite clear:
I don't want to use submodules or crutches such as renaming all
.git/in subdirectories.
I just want that Git treats my.git/subdirs as plain dirs with any other names.
Brian m. Carlson (bk2204) answers:
This is not possible.
You can't add a non-bare repository as a part of a parent repository without using submodules.
Git uses the.gitdirectory to find the working tree and for safety reasons doesn't allow files or directories named that to be checked in.
Allowing users to check in.gitdirectories would allow configuration and hooks to be stored as part of the repository, which would allow the execution of arbitrary code if someone cloned it and then changed into the subrepository.
3 Comments
I found another method that seems to work for me
If you git add somefolder/ <-- make sure you have the / on the end, Then it will add all the files instead of treating it like a submodule.
More is mentioned here: http://debuggable.com/posts/git-fake-submodules:4b563ee4-f3cc-4061-967e-0e48cbdd56cb
4 Comments
error: Invalid path 'test/data/fixtures/clean/.git/COMMIT_EDITMSG' error: unable to add test/data/fixtures/clean/.git/COMMIT_EDITMSG to index fatal: adding files failed It does seem to work with the actual files though, thanks, it's a start.git folder to .checkout_git blog.gopheracademy.com/advent-2015/go-in-a-monorepo And rename it back as as neededSee also Are git submodules the only safe way to have working copies within working copies? .
Actually, when I came across the current discussion, I was concerned with a problem different from the on solved here: using git as a kind of archiving tool that could archive a filesystem tree which already has some git working directories... perhaps the other question is closer to my problem.
2 Comments
One of the most simple (and dirty) thing you can do is renaming the .git folder
I had this folder structure:
|- .git |- folders |- second-repo |- .git |- folders I simply used this bash command from root folder:
mv /.git /.not_git Now you can go inside the second-repo and use its own git for commands and in this way you can go back and forth to use both git repos.
PS: Git subomodules is the correct way to handle this but in my case I couldn't go for them