44

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.

stevo
  • 443
  • 1
  • 4
  • 4
  • 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 Feb 23 '10 at 12:04
  • 1
    It sounds like what you really want is a subtree merge. That will leave you with a single repository and a single history. –  Aug 25 '11 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 Jul 13 '16 at 05:04

5 Answers5

8

1/ You could override that through:

  • either a git config setting: set the environment variable $GIT_DIR
    you define your .git directory 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 .git directory 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 .git directories 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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
8

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

Drew LeSueur
  • 19,185
  • 29
  • 89
  • 106
  • This is what I was looking for, but I get an error message if I try to add the nested .git itself: `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 –  Jul 07 '16 at 16:02
  • 1
    I've seen where people rename a nested `.git` folder to `.checkout_git` https://blog.gopheracademy.com/advent-2015/go-in-a-monorepo/ And rename it back as as needed – Drew LeSueur Jul 07 '16 at 16:37
  • 2
    This no longer seems to work, git warned about adding a nested git repository and tried to add it like a submodule for me. – saagarjha Feb 08 '20 at 11:21
  • This seemed to work for me still (Jan 2021) – redfox05 Jan 13 '21 at 11:09
2

See 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.

Community
  • 1
  • 1
imz -- Ivan Zakharyaschev
  • 4,921
  • 6
  • 53
  • 104
  • Here is a tool targeted at my problem: http://webcache.googleusercontent.com/search?q=cache:Ffr9y2l0jh4J:eigenclass.org/hiki/gibak-backup-system-introduction+nested+git&cd=2&hl=en&ct=clnk&gl=ru . There are probably other similar tools. – imz -- Ivan Zakharyaschev Jun 23 '10 at 18:53
  • 1
    oops - broken link in the comment (which you probably should delete and add to your answer) – Mr_and_Mrs_D Sep 18 '14 at 20:06
0

This isn't the most elegant solution, but it's worth mentioning.

If you zip up the folder containing the repository you can check that into the outer repository.

This can be helpful because you'll have the full complete state including git reflog.

funroll
  • 35,925
  • 7
  • 54
  • 59
0

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

MatPag
  • 41,742
  • 14
  • 105
  • 114