Git: Where exactly is the "working directory"?
Asked Answered
F

7

45

I am going through some Git tutorials. The concept of a "working directory" keeps being mentioned, however, none of the tutorials or documents I read points out where or what this "working directory" is.

I have thought that it was actually the .git's parent directory, a.k.a the directory I run git init in. But the video tutorial I am watching talks about the state of nothing to commit and "working directory clean":

In fact you can actually make a copy of the repository, and make that copy so that it does not have a working directory, this is actually called the bare clone. This is actually what GitHub uses.

If my understanding of the "working directory" is correct, how can a repository not have a "working directory"? And what does it mean, when it says that GitHub uses a "bare clone"?

Flintshire answered 24/3, 2016 at 13:27 Comment(4)
It's covered here: "The working directory is a single checkout of one version of the project. These files are pulled out of the compressed database in the Git directory and placed on disk for you to use or modify".Liar
I read this already. maybe its my English, but I just don't understand. Which directory is working directory exactly?Flintshire
The directory named .git is your git directory and the one in which .git and all other files resides is working directory (if you created a project locally then the directory in which you run the command git init. If you downloaded the project then the single checkout of the project).Oosphere
FWIW, the "working directory" terminology is really confusing to me. If it were named "working area" alongside "staging area" then it would be less confusing to me.Constellation
B
20

This should hopefully clear things up for us:

What is the difference between a repository created using the git init command and the git init --bare command?

Repositories created with the git init command are called working directories. In the top level folder of the repository you will find two things:

A .git subfolder with all the git related revision history of your repo
A working tree, or checked out copies of your project files.

Repositories created with git init --bare are called bare repos. They are structured a bit differently from working directories. First off, they contain no working or checked out copy of your source files. And second, bare repos store git revision history of your repo in the root folder of your repository instead of in a .git subfolder. Note… bare repositories are customarily given a .git extension.

Taken from John Saints - What is a bare git repository?

A bare git clone does not contain a working directory of checked out code, in other words.
Think of it as just the .git directory (the Git database) without anything else.

Bazluke answered 24/3, 2016 at 13:50 Comment(2)
Thanks. I start to understand. If Github is using bare clone and bare clone is like .git directory, why is there no head,index,hook in Github's repo?Flintshire
FWIW, the "working directory" terminology is really confusing to me. If it were named "working area" alongside "staging area" then it would be less confusing to me.Constellation
C
14

It's wherever you have checked out the project. For example the directory within which you have checked out a branch of your project. It's typically the folder that contains the .git folder. That is the working directory. When you make changes to files in your checked out branch you make changes to the working directory. At this point the working directory has uncommitted changes. So initially, when you haven't made any commits, the working directory will be clean as there are no changes.

Christychristye answered 24/3, 2016 at 13:34 Comment(0)
V
8

To kind of combine the two other answers:

As stated in the Git Documentation:

The working directory is a single checkout of one version of the project.

This essentially means if you checkout a branch (e.g. master) and are sat on a particular commit (e.g. HEAD), your working directory is the "umbrella" term for all your files and folders.

It isn't a particular directory/folder though. The working directory covers all directories, files...everything.
I mention this because when you want to commit some files, those files will be in the working directory and you'll need to stage them (using git add) before committing them (using git commit).

Vargo answered 24/3, 2016 at 13:50 Comment(1)
This is not strictly correct. When you run the git status command, git compares a specific "working directory" to the state of your Index and the HEAD of your branch. Likewise, unless you have set some specific environment variables during git init, if you attempt to git add from a location outside of the root directory of your .git folder then you will receive an error telling you that you need to return to the working directory to run the command. So while I agree there is a conceptual "working directory", the Git implementation also tracks a physical location.Doxia
S
8

The working directory is simply, your current local directory that you are working on. e.g if you have master, dev and yourname-dev as your remote branches, if you checkout from dev to yourname-dev, yourname-dev is now your working directory if you checkout from this (yourname-dev) working directory to another say dev, dev is now your new working directory

Sisco answered 24/3, 2016 at 14:17 Comment(0)
H
4

According to the documentation:

Finally, you have your working directory. The other two trees store their content in an efficient but inconvenient manner, inside the .git folder. The Working Directory unpacks them into actual files, which makes it much easier for you to edit them. Think of the Working Directory as a sandbox, where you can try changes out before committing them to your staging area (index) and then to history.

Humility answered 22/3, 2017 at 3:33 Comment(1)
Good catch. I want to underline the importance of these sentences - "The Working Directory unpacks them into actual files, which makes it much easier for you to edit them" - "Think of the Working Directory as a sandbox"Philip
E
2

To answer OP's 3 questions:

Q1: Where exactly is the "working directory"?
A1: "A Git project normally consists of a working directory with a '.git' subdirectory at the top level." (source: https://git-scm.com/docs/git#_discussion)

Q2: How can a repository not have a "working directory"?
A2: A Git "bare repository" is one example (source: https://git-scm.com/book/en/v2/Git-on-the-Server-Getting-Git-on-a-Server#_getting_git_on_a_server)

Q3: And what does it mean, when it says that GitHub uses a "bare clone"?
A3: Read the documentation for the --bare option on git clone, here: https://www.git-scm.com/docs/git-clone

Ecclesiastic answered 17/3, 2021 at 16:20 Comment(1)
This is a perfect answer. Or, almost-perfect: it would be useful to have a link to saintsjd.com/2011/01/what-is-a-bare-git-repository to better indicate why you would want such a beast as a bare repository. "To get git on a server" didn't really do it for me the way this other link did.Hrvatska
E
0

Do you work directly in the local repos?

When I follow Microsoft's instructions [ref. https://learn.microsoft.com/en-us/azure/devops/repos/git/clone?view=azure-devops&tabs=visual-studio ], that's where I end up opening the .sln solution file. That seems wrong to me and unfortunate, because I prefer to work on code on my massive D: drive (D:\dev) rather than in C:\Users\\source\repos, where the repos are kept by default (I don't mind the local repos being kept there - it's not ideal - as long as I can work in my D:\dev area).

Eighteenmo answered 29/3, 2019 at 9:23 Comment(1)
I will attempt to answer my own question: Yes, you work in your local repos(itory). All of the local git "stuff" is placed in the .git folder in your "local repos" a.k.a. "working folder/directory". Folder/directory here is a folder that can contain other folders/directories - as well as files - indeed it will always contain a .git folder, otherwise it would not be a repository.Eighteenmo

© 2022 - 2024 — McMap. All rights reserved.