What is a Git repository?
It's a folder where Git has been initialized. It's where we can run Git commands. It's where we can use Git to keep track of the other files in the folder.
Git repositories can be local (in your computer) or remote (in someone else’s computer).
All the Git repository information is stored in a folder called .git
. When you initialize Git in a folder (such as your project), the .git
folder is created. We’ll look at what’s inside .git
shortly, but we can think of .git
as essentially a database. Git is a tool we use to interact with that database.
Commits
The main thing that Git stores in .git
are commits. A commit is a snapshot of your project folder. It contains:
- A copy of all files you have changed since the last commit
- A reference to all files you haven’t changed since the last commit (but that you committed at some point in the past)
Working area
The .git
folder is inside your project folder. Before we tell Git about a file, we say that file is in the working directory. That is, we’re working with it, but we haven’t stored it in the database yet.
Staging area
Let’s say we then make some changes to a file. We can tell Git we want to include those changes in the next commit.
This adds the file to the staging area.
Committed
Once we’ve added all the changed files we want to include in the next commit to the staging area, we can commit them!
This stores all the new files, and references to all previously committed files, in a new commit.
Once a file has been committed at least once, Git will start keeping a reference to it in future commits. This is called “tracking”.
Remote
A remote is a copy of our local repository stored on another computer.
At any time we can push our commits to the remote to update it. This serves as a backup in case we lose our local repository. It's also good for collaborating with other developers.
Overview
Working directory -> Staging area -> Committed -> Remote
After committing, files are tracked.
The process can be reversed for each commit, we will learn how soon!
How to initialize a Git repository
After installing Git1, you can use the console to initialize a Git repository in any folder. That will allow you to start tracking files (we'll discuss how in the coming sections).
To initialize a Git repository, use this command:
git init