Search code examples
gitdirectoryworking-directory

How can there be no working directory in git bare?


I'm reading from this article here and don't understand how there is not working directory when you git init --bare. How can there be no working directory? Isn't that the directory where all your files reside? When you create a file in your repo doesn't that start a working directory anyway?

I also don't understand the 2nd bullet, which is:

There is no .git directory created. Instead, the files normally in the .git directory are placed in the top-level directory where the working directory would normally be.

What is the purpose of not having a .git directory? How does this result in the author's example about the creator of a non-bare repo doing git status and seeing that

git would find a different object database and give different results for this run of git status.


Solution

  • The distinction between a bare and non-bare Git repository is misleading and can confuse newcomers.

    How can there be no working directory? Isn't that the directory where all your files reside?

    No. Actually all the information (ie. all the data that form a git repository) is stored in the .git/ folder that resides in your project's root directory (assuming it's a non-bare repo) and that is all Git itself needs to create a working directory.

    The working directory does not hold any state regarding the repository (eg. branches, tags, commits) and technically is not a part of it (ie. it's not necessary). It's just there for a way for the programmer to edit the files.

    It may help to understand that when you do a regular clone from a repository on, say, Github, the repository residing on Github's servers is a bare repository, but what you get is a non-bare repository.

    Now onto your 2nd question.

    What is the purpose of not having a .git directory?

    Think about this: What is the purpose of a working directory in a git server? The repositories stored in servers do not need the working directory, because noone is going to just SSH into a git server and directly edit the files there.

    So the repository structure is simplified by keeping only what's necessary, which is the contents of the .git/ directory. Furthermore the contents of the .git/ directory are put in the project's root folder since there would be no much sense in keeping them in .git/ since no other files are present in the root directory.