Can you use Git as SCM without the .git folder!!!

Asutosh Panda
3 min readMar 25, 2022

For the impatient-Yes..you can use Git as a Source Code Management tool without even using the .git folder inside your repository. But what is the need? Why are we even talking about it. I have worked on a migration which was implemented on 5 instances and all of them were sharing the same license. The license and the security protocols were somehow getting into a fight with the .git folder and we were unable to implement Git for months. So we decided to find a way around to use Git without the .git folder present inside the repo itself.

So for this kind of unique scenarios this method can be used or else you use this to have a centralized repo that will hold the history of multiple repos also.

Let’s get started

To implement this way around you have to have a good idea about Bare Git Repository. The point to note about bare repo is as the name suggests it’s just a bare bone of a actual git structure, it doesn’t support all the git commands. It only supports few commands of Git. But after this whole procedure the bare repo will be able to execute all the Git commands that a normal Git repository can execute by default.

The logic is a git repo works using 2 components-

  1. Working Tree : this holds the codes, files etc that actually contains in the repo that you have created
  2. History : this holds the metadata using which git manages the SCM.
logical diagram : before the way around implementation
logical diagram : after the way around implementation

These 2 components contain in one folder by default. Here we can modify and put these two components in 2 different folder, then we can arrange a link between these 2 folders and voila.. SCM will work fine. Let’s quickly go through the steps on how to implement this :-

  • Create a bare repo in a folder where you will hold the history
git init --bare C:/Users/Asutosh/Documents/HistoryFolder
  • Now specify which folder will be holding the snapshot/working tree using git command. But this command you have to repeat every time you will use git. It will be irritating to use such a long command, so we will be setting up an alias for the whole git command. Only adjustment we have to do is use the alias name each time instead of using “git” command(pro guys will set up the alias name also as git😜).
alias AliasName='git --git-dir=C:/Users/Asutosh/Documents/HistoryFolder --work-tree=C:/Users/Asutosh/Documents/WorkingTreeFolder'
  • From now on you have to use the “AliasName” as a replacement of “git” command. Following that we have to inform Git to not to display all the untracked files or else it will show everything present in the directory where your “HistoryFolder” exists. It should only show the untracked files present in your “WorkingTreeFolder” whenever you’ll use git status
AliasName config status.showUntrackedFiles no
  • Then we can follow all the usual procedures that we do while using git, setting up a remote, push etc. All the commands from here onwards you have to run inside the “HistoryFolder”. So let’s set up a remote and push to GitHub from this folder.
AliasName remote add origin git@github.com:<username>/<reponame>.git
AliasName add ~/.gitconfig
AliasName commit -m "commiting without the .git folder"
AliasName push origin master

cool..isn’t it!

Here are few resources if you want to learn more about this

thank you for reading upto here😊

--

--

Asutosh Panda

I am a DevOps Engineer, interested in SRE and DevOps world, apart from tech I am into cinematography, poetry, dance