41

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"?

Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
shenkwen
  • 3,536
  • 5
  • 45
  • 85
  • 5
    It's covered [here](https://git-scm.com/book/en/v2/Getting-Started-Git-Basics): "*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*". – Maroun Mar 24 '16 at 13:29
  • 1
    I read this already. maybe its my English, but I just don't understand. Which directory is working directory exactly? – shenkwen Mar 24 '16 at 13:34
  • 3
    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). – haccks Apr 05 '18 at 14:07
  • 1
    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. – Elijah Lynn Jul 19 '18 at 03:57

7 Answers7

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.

jacmoe
  • 1,637
  • 13
  • 17
  • 1
    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? – shenkwen Mar 24 '16 at 14:05
  • 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. – Elijah Lynn Jul 19 '18 at 03:56
13

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.

nCardot
  • 5,992
  • 6
  • 47
  • 83
Som Bhattacharyya
  • 3,972
  • 35
  • 54
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

Babajide Apata
  • 671
  • 6
  • 18
7

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).

Harmelodic
  • 5,682
  • 1
  • 28
  • 31
  • 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. –  Jun 15 '17 at 17:41
3

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.

Michael Ma
  • 872
  • 8
  • 20
  • 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" – Remo Harsono Dec 20 '18 at 05:12
1

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

dave_k_smith
  • 655
  • 1
  • 7
  • 22
  • 1
    This is a perfect answer. Or, almost-perfect: it would be useful to have a link to https://www.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. – fool4jesus Aug 25 '21 at 15:40
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).

Zeek2
  • 386
  • 4
  • 8
  • 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. – Zeek2 Apr 09 '19 at 07:38