Multiple Git repos in a single directory

Tags: git, short

There might be a case when you want 2 different Git repositories in a single directory. The most logical situation which comes to mind is keeping 2 repositories in your home-folder, one to perhaps keep your configuration files, and another to keep your custom scripts.

Seems out-of-bounds right? Not really. It so happens that Git offers a ‘git-dir’ flag to all its commands which tells where the Git files for that repository are stored. Here’s how to put it to some real use.

  1. Firstly, do a git init in your home folder. This creates a folder ‘.git’ in your home folder.

  2. Go move it to say ‘.rcgit’ with mv .git .rcgit.

  3. Do a git init again. Git won’t know there’s another repo there in rcgit so it works fine. Move this new ‘.git’ folder to say ‘.scgit’.

  4. Now edit your ‘.zsh_aliases’, ‘.zshrc’ or ‘.bashrc’ to include these 2 lines

Add to aliases:

alias rcgit='git --git-dir=.rcgit'
alias scgit='git --git-dir=.scgit'

As you may have probably guessed by now, you can use the 2 repositories by using ‘rcgit’/‘scgit’ instead of ‘git’ in their parent folder.

Note that this won’t work if you try these commands from any of their children folders. You’ll have to give the –git-dir flag manually in that case.

Another thing to keep in mind is that you would have trouble keeping 2 separate ‘.gitignores’. Hopefully the 2 repositories would be able to share a single one.

For another powerful functionality in Git, look at submodules. Some other day perhaps.

Author's picture

Saksham Sharma

Hacker. Programmer. Geek.

3rd year Undergrad, Computer Science and Engineering

Indian Institute of Technology Kanpur