HowTo: Using multiple git repositories under the same directory
If you are to work under two different repositories with different commit history, you may either,
- Copy the changes between multiple git repo folders, back and forth.
- Use the same directory separately for different repositories.
First one easy enough but it gets frustrating as the project evolves. Today I’ll tell you how to use any number of repositories under the same directory.
.git directory under the project root is the git repository information. It consists everything regarding to the repository. Good news is, its name can be changed.
- Clone your repositories to different directories first.
- Rename your .git to something you like, .gitRepo1, .gitRepo2 maybe? I’m bad at naming things. Figure this out.
- Copy all your .git dirs under the same directory where you like to work.
- Now comes the tricky part. Whenever you want to use one, go like this: “git –git-dir=.gitRepo1”
Well, sure that works but it’s a hassle to write
--git-dir=[reponame] at all times. Fear not, we can create an alias for that.
Setting Alias for easier use
I’m using ConEmu these days. It’s a great command line wrapper for Windows. I recommend you to use it as well. If you’re on Linux,
- Download ConEmu
- Go to Settings (
Win + Alt + P)
- Go to
- Add your alias in this form:
alias mygit=git --git-dir=.gitRepo1 $*
$* ensures that your arguments after
mygit will be passed to git.
Thanks Murat for his comment how to use alias in Linux. Add this alias command to your profile and you can just type
mygit to use your
echo "alias mygit='git –git-dir=.gitRepo1'" >> ~/.profile
It should be same as Linux since it’s a UNIX OS.
To use it, just type your alias and your usual git commands, like this:
mygit status -s
That’s it. Enjoy your multiple repositories.