2

Recently, I discovered that git could be initialized with a --bare flag (e.g. git init --bare). It creates a local repository without a working tree (workspace). As far as I understand, such repositories can be used as a remote from which you can clone, pull and push.

Do GitHub and similar services use "bare repositories" under the hood?

I'm wondering because whenever I create a local repo and push it to a remote service (like GitHub, GitLab, Bitbucket, etc.), I don't create the bare repo myself and I'm not sure if it was created at all (implicitly by GitHub). If it wasn't created, then what is tracking the files/history?


tl;dr

Locally, I can do something like this

-- Step 1. Create a local bare repository

$ mkdir bare-repo/
$ cd bare-repo/
$ git init --bare

-- Step 2. Clone a non-bare repo and push changes

$ mkdir non-bare-repo-1
$ cd non-bare-repo-1
$ git clone ../bare-repo .
$ touch file.txt
$ git add .
$ git commit -m "Add file.txt"
$ git push (it pushes changes to the local remote a.k.a. non-bare/)

And I can create another folder and another and all of them will refer to a single (local) bare-repo/ which tracks changes of all cloned repositories.

I wonder if GitHub uses this approach under the hood, or if the bare repositories are used for something else...

Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68
  • 1
    A bare repo is basically nothing else than the content of the `.git` repository inside a non-bare repo (i.e. holding a working copy). Holding a working make some push operation dangerous. Github, Gitlab, Gitea.... do not hold working copies. So basically yes, they store bare repositories for your project. I can tell your for sure that Gitlab and Gitea store pure git bare repositories on their file system since I deployed each of them at least on test environments. I can't check for sure for Github since it is not opensource nor installable on premise. But I'd be surprised if it isn't the case. – Zeitounator Jul 02 '22 at 12:26

2 Answers2

1

Answering my own question

Do GitHub and similar services use "bare repositories" under the hood?

Yes

It looks like bare git repositories were designed to be used as remote instances and GitHub has to use them to get it to work.

P.S.


Related answer by @VonC

It seems that GitHub repository can work as working repository and bare repository at the same time, does that mean GitHub repository is developed from git bare repository

No, Git repos on GitHub are bare, like any remote repo to which you want to push.


Related answer by @duncan

Short answer

A bare repository is a git repository without a working copy, therefore the content of .git is top-level for that directory.

Use a non-bare repository to work locally and a bare repository as a central server/hub to share your changes with other people. For example, when you create a repository on github.com, it is created as a bare repository.

So, in your computer:

git init
touch README
git add README
git commit -m "initial commit"

on the server:

cd /srv/git/project
git init --bare
Then on the client, you push:

git push username@server:/srv/git/project master

...

Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68
0

I believe so. You can in fact create a GitHub repo and deselect all the auto created files (e.g. README.md, LICENSE). You're provided a list of instructions to init, commit and push the repo.

enter image description here

astrochun
  • 1,642
  • 2
  • 7
  • 18
  • Even If I deselect all files and press a create repository, it should create a non-bare repository with an empty worktree. The `.git` folder will exist, but the bare repo must not have a `.git` folder, only its internals. – Roman Mahotskyi Jul 02 '22 at 13:03
  • Fair point. It's an "empty repo" and not a "bare repo": `warning: You appear to have cloned an empty repository.` – astrochun Jul 02 '22 at 13:07