Git Question: What is the difference between the working directory (aka workspace) and the repository?
The repository is essentially the .git hidden folder inside the working directory (workspace).
The working directory (workspace) is essentially your project folder.
Also note the term directory is basically synonymous to the term folder.
Let me show the difference with an example.
Step 1: Creating a working directory/workspace
Assume you create a project folder locally on your computer. Make the folder in whatever way you like, below are two examples.
Then what you have is this, a project folder called ‘amazing-project’.
At this point amazing-project is NOT a repository.
Right now it is just a working directory (workspace). It is just a project folder.
Step 2: Creating a repository
Now assume you want to make amazing-project into a repository so that you have a local repository.
In order to do this you will need to use the ‘git init’ command.
This command will create a hidden folder inside your amazing-project folder called ‘.git’. This is essentially your repository.
Now what you have is this:
A .git directory (.git folder) inside your amazing-project directory (or workspace).
Step 3: The working directory and repository in depth
Now let’s look at the workspace and repository in more depth.
We can distinguish between the two.
The amazing-project folder like we said represents our working directory:
And the .git folder represents our repository:
And actually within our repository there are in a way two important place to keep in mind. The staging area and the commit history.
Step 4: Adding a file
Now let’s add the first file, I’ll call it example.txt. Add the file in whatever way you like. You should end up with:
So using our diagram this is:
So, the file is in our working directory (workspace).
But this file is NOT part of our repository. This file is NOT yet being version controlled.
You can see this explicitly if you run the ‘git status’ command.
Git literally tells you this file is untracked.
Step 5: Adding the file to our repository
In order for this file to come part of our repository and to start being version controlled we need to let Git know about it by adding it to the staging area using the ‘git add’ command.
So then what we have is:
The ‘git add’ command copies the file from the working directory to the staging area.
WARNING: A common misconception is that the file is moved, that is not the case. The file is copied.
Now the file is being tracked by Git.
Hopefully the above helped to clarify the difference between the working directory and the repository! If you like the way I explain things then please do check out my online course on Udemy that teaches Git version control!
I also explain some of the concepts above in the first couple lessons of my course which can be found in the below videos:
You can also :
- Check out my YouTube channel on which I have the first 8 lessons of my course! 📹
- Follow me on instagram @techjourney_with_anna 😊
- Join my mailing list ✉️
- Follow me on Eventbrite 🗓
- Connect with me on LinkedIn 💼
- Or just shoot me an email: annaskoulikari@gmail.com
- Bonus: For any French speakers 🇫🇷 out there, my course is also available in French!