Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is repository different from workspace?

Tags:

git

github

I'm afraid my question will be a down-voted or marked duplicate. But I still want to clear my basics of git. There are articles on the internet, but since I'm only a few days old in git, I'm confused here:

enter image description here

Clearly, Repository and Workspace are two different things. So I googled the difference between the two. Here is the answer: A local repository is a directory within your workspace. I did not understand this in the light of git. Here is a similar question on StackOverflow. But I did not understand the answer very well. Then I tried studying this: enter image description here.

Please explain to me this as simple as possible (*assume you're explaining this to a 10-year-old kid). I may not understand heavy git jargon.

like image 674
Tanzeel Avatar asked Dec 22 '19 04:12

Tanzeel


2 Answers

The repository is essentially the .git hidden folder inside the working directory (workspace).

The working directory (workspace) is essentially your project folder. 

Keep in mind these are basically synonyms: workspace = working directory = project 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. I'm going to call mine amazing-project.

Then what you have is this, a project folder called 'amazing-project'.

enter image description here

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:

enter image description here

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.

enter image description here

The amazing-project folder like we said represents our working directory:

enter image description here

And the .git folder represents our repository:

enter image description here

And actually within our repository there are in a way two important place to keep in mind. The staging area and the commit history.

enter image description here

Step 4: Adding a file 

Now let's add the first file, I'll call it example.txt.

So using our diagram this is:

enter image description here

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.

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:

enter image description here

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. And if you were to commit now it would be included in your commit.

Hopefully that explained a bit off the difference between the repository and the workspace! This video 📹 and this video 📹 were really helpful in explaining this as well as this amazing article 📝 which gives a couple more details!

like image 105
Anna Skoulikari Avatar answered Oct 05 '22 09:10

Anna Skoulikari


They are talking about repository and working tree. So, on a normal local directory that you are tracking with git you have the working tree (the workspace they are talking about) with all the files that make up your project.... and you have a special directory in there called .git. That's the repo with all the git stuff that makes it tick.... and the index is one of the files that is part of the repo and it (the index, aka staging area) is one of the basic features of git.

like image 33
eftshift0 Avatar answered Oct 05 '22 07:10

eftshift0