问题
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"?
回答1:
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.
回答2:
Its is wherever you have checkout the project. For example the directory within which you have checked out a branch of your project. Its is 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.
回答3:
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
回答4:
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
).
回答5:
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.
回答6:
Do you work directly in the local repos?
When I follow Microsoft's instructions [ref. https://docs.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).
来源:https://stackoverflow.com/questions/36201342/git-where-exactly-is-the-working-directory