Two git repositories in one directory?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The short answer is that one filesystem directory cannot cleanly be the working tree of two unrelated Git repositories at the same time. What Git supports well is a directory tree that contains a repository at the top level and another repository inside a subdirectory, or multiple working trees for the same repository using git worktree.
What Git actually allows
Git identifies a repository by its .git directory or .git file. Once a directory is already the working tree for one repository, pointing a second unrelated repository at the exact same tracked files is not a normal or well-supported setup.
There are three common patterns that people really mean when they ask this question:
- a nested repository inside a subdirectory
- a submodule managed by the parent repository
- two separate checkouts sitting next to each other as sibling directories
Those are all valid. "Two unrelated repos controlling the same directory contents" is where trouble starts.
Nested repository inside a subdirectory
A simple valid layout is a parent folder containing one repository, plus a child folder that is its own repository.
Now project/.git controls the parent repository, and project/plugin/.git controls the nested one.
This works, but the parent repository will usually see the nested directory as just a directory unless you intentionally track it as a submodule. If you accidentally add the nested repository contents from the parent, you can create confusing states.
When a submodule is the better answer
If the inner repository is supposed to remain an independent project but still be referenced by the outer project, use a submodule.
With a submodule, the outer repository stores a pointer to a specific commit of the inner repository. That is much cleaner than copying files around or trying to make one directory belong to two repos at once.
Submodules are useful when:
- the nested code has its own lifecycle
- the outer repo should pin a specific version
- the nested code needs its own remote and commit history
They do add workflow overhead, so use them deliberately.
When you really want sibling repositories
Often the cleanest fix is not nesting at all. Put the repositories beside each other.
This avoids accidental cross-tracking, confusing .gitignore behavior, and awkward tooling assumptions. If the two projects are truly separate, sibling repositories are usually simpler than any shared-directory trick.
git worktree is for one repo, not two unrelated repos
People sometimes discover git worktree and think it solves the "two repos in one directory" problem. It does not. git worktree lets one repository have multiple working trees, usually for different branches.
Now both directories belong to the same repository history. This is excellent for parallel branch work, but it is not a way to merge two unrelated repositories into one folder.
What not to do
You can technically abuse GIT_DIR, GIT_WORK_TREE, or manual .git file tricks to point different repository metadata at overlapping files. That tends to create fragile setups where status, ignore rules, and tooling behave unpredictably. Editors, CI scripts, and teammates will all have trouble understanding what is supposed to happen.
If you need shared history between projects, use a monorepo, submodule, subtree, or separate repositories with automation between them. Those are stable patterns. Overlapping Git control is not.
Common Pitfalls
A common mistake is nesting a repository and then forgetting that the parent repo can still try to add files from that directory. Decide whether the nested repo should be ignored or tracked as a submodule.
Another mistake is using submodules when the team is not prepared for the extra workflow. Submodules are valid, but everyone has to understand update, clone, and commit behavior.
A third mistake is using git worktree for the wrong problem. It solves multiple checkouts of one repo, not multiple unrelated repos in one working tree.
Summary
- One directory should not normally be the working tree of two unrelated Git repositories.
- Nested repositories inside subdirectories are valid, but they need careful handling.
- Use submodules when the parent repo should reference an independent child repo.
- Use sibling repositories when the projects are simply separate.
- Use
git worktreeonly when you need multiple checkouts of the same repository.
Related reading
- Two Phase Commit blocking on coordinator failure
- Two phase commit what happens if the coordinator dies between sending two confirmations
- Unable to merge dex
- Unable to resolve unable to get local issuer certificate using git on Windows with self-signed certificate
- Unable to run Kafka - getting no output in cmd and No such file or directory error in Git Bash
- Unable to show a Git tree in terminal
- Unable to use summary.merge in tensorboard for separate training and evaluation summaries
- Undo a Git merge that hasn't been pushed yet
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.