15

I want to maintain two different git repos. The repos should stay in the same root directory. How to achieve it?

What I'm want is: to manage two repositories that differ slightly. Can I have two completely different repositories in the same directory?

yivi
  • 42,438
  • 18
  • 116
  • 138
Shafiul
  • 2,832
  • 9
  • 37
  • 55
  • 1
    possible duplicate of [Multiple Git repositories in one directory](http://stackoverflow.com/questions/2519446/multiple-git-repositories-in-one-directory) – Dave Newton Jan 08 '12 at 15:41
  • 1
    And [here](http://stackoverflow.com/questions/2065559/using-two-git-repos-in-one-folder), and [here](http://stackoverflow.com/questions/436125/two-git-repositories-in-one-directory), and [here](http://grahamc.com/blog/multiple-git-repositories-one-directory-dotfiles/), ... – Dave Newton Jan 08 '12 at 15:41
  • The first post doesn't exactly address this issue. What it addresses is sub-moduling. What I'm asking is: Can I create two individual repos based on same source files? – Shafiul Jan 08 '12 at 15:51
  • 9
    If you came here from google: the 4 possible duplictes from the comments above address submodules or branches. The accepted answer below has a different solution with 2 absolutely unrelated repos in the same directory. – Alojz Janez Jun 16 '13 at 17:05
  • 2
    Possible duplicate of [Two git repositories in one directory?](http://stackoverflow.com/questions/436125/two-git-repositories-in-one-directory) – Tobias Kienzler Mar 28 '17 at 07:54
  • @DaveNewton 's last "here" link rotted on us, and I found the Internet Archive for it: https://web.archive.org/web/20141221053421/http://grahamc.com/blog/multiple-git-repositories-one-directory-dotfiles/ – bgoodr Nov 14 '21 at 18:07

2 Answers2

13

You can achieve this by adding using one of these two options on the git command itself:

git --work-tree=where/my/code/is --git-dir=some/path/to/my/.git status

This would allow you to have 2 separate repos share the same working folder.

However you should be able to get what you need by using one repo with multiple branches and perhaps only push certain branches to multiple remotes.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • Yup, I may end up with multiple branches pushing selected brunches only. – Shafiul Jan 09 '12 at 02:53
  • 2
    I also made a script that makes it easier to work with overlaid repos, and can be used as a package manager too: https://github.com/capr/multigit –  Apr 09 '15 at 05:54
2

I solved this problem, which git doesn't seem to provide a good solution to, by not using git to solve it. I used a directory junction to link a new subfolder to a subfolder of the parent folder (i.e. linking a child folder to an "uncle folder"). Example in Command Prompt for Windows Vista and up:

cd CurrentFolder/
mklink /J "LinkedFolder" "../TargetFolder"

will cause LinkedFolder to point to TargetFolder (note the quotes). Example file structure I would then use:

  • root/
    • TargetFolder/
      • shared files
    • CurrentFolder/
      • LinkedFolder/

"POSIX-compliant" operating systems seem to use ln or ln -s for this functionality.

It works excellently (note: the following is from my own Windows 8.1 testing):

  • the LinkedFolder does not exist before calling mklink
  • when you make the link, anything you do to the files in either the TargetFolder or the LinkedFolder will reflect in the other, as they are one and the same
  • if you delete the link (LinkedFolder), nothing happens to the actual target folder (TargetFolder)
  • if you delete the actual target folder (TargetFolder), the link will remain active (it does not get deleted); if you then try to access the link, you will simply get an error; if you recreate the actual target folder (TargetFolder) again, the link will continue to work as before!

See also:
NTFS Junction Point
NTFS Symbolic Link
Symbolic Link

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Andrew
  • 5,839
  • 1
  • 51
  • 72