Today, I’ll show how to keep git repository in another git repository. It may sound crazy and unnecessary. Yet, I’ve stumbled once on a need to do that during my career. So, I’ll share the way with the world. If you later find this article useful take a look at the disclaimer for information on how to thank me.
Why Store Git Repository in another Git Repository?
If you came across this article, you probably know why you need to do such a bizarre thing. Yet, for those who wonder why one might need to store a git repository inside another git repository, I’ll provide an example.
Suppose, your app needs to pull some code from a git repository (e.g. some script) and run it. So far, it seems like an easy use case. You test your app on a dev environment. Nicely, it has access to an org git repository hosting. Thus, there’s no issue to place your scripts in some git repository and provide repository url and credentials to your app. Now, suppose your app runs in an air-gapped environment for security reasons. In that case, the app obviously won’t have access to git repository hosting and must use a different solution. You might say, why require git access in the first place. It’s a rightful question for the apps you fully control. Yet, what if you use some 3rd party product which you cannot modify? I stumbled on such a use case while using AWX.
Real world use-case requiring storing Git Repository in another Git Repository
Simply put, AWX is a web app for managing ansible playbooks. Ansible requires an inventory of hosts to run the playbooks on. And such an inventory may come from a dynamic inventory script. AWX pulls this script from a git repository. That’s how it works and I can do nothing about it. Now imagine, AWX running in production air-gapped environment. How would you provide access from AWX to a git repository? Short things short, I came up with the solution of keeping an inventory script inside a bare git repository which resided in the code repository of AWX out of which its deployable artifact was built. Let’s see git commands I used to create bare git repository inside another repository.
How to store Git Repository in another Git Repository?
To achieve that, you need to use bare git repository. This way you store the repository as files. So they can be source controlled just as any other data you want to store in git. Follow below commands to store git bare repository (
inner below) inside another git repository (
- Create outer git repository:
mkdir /tmp/outer cd /tmp/outer git init echo "i'm outer" > outer.txt git add * git commit -am 'outer'
- Create inner git repository:
mkdir /tmp/inner cd /tmp/inner git init
- make sample change inside
echo "i'm inner" > inner.text git add inner.text git commit -am 'test'
innerrepo as a bare repository insider outer
cd /tmp/outer/ git status git clone --bare /tmp/inner /tmp/outer/inner
- inspect the contents of
/tmp/outer/and convince yourself that the contents are nearly the same as the contents of
cd /tmp/outer ls branches config description HEAD hooks info objects packed-refs refs ls /tmp/inner/.git/ branches COMMIT_EDITMSG config description HEAD hooks index info logs objects refs
cd /tmp/outer git add * git commit -m 'inner inside outer'
That’s it! You can now push outer repository, build a deployable artifact out of it and deploy it to production. Anyone or anything will be able to clone
inner repository to get its contents:
git clone /tmp/outer/inner /tmp/inner_contents cat /tmp/inner_contents/inner.text i'm inner
If you succeeded to follow, then you might really needed this to work as I did 🙂
That’s it about keeping git repository in another git repository. Feel free to share.
If you found this article useful, take a look at the disclaimer for information on how to thank me.
You can find below articles useful as well: