Lesson 2: Start a repo and make your first commit
Lesson objectives:
- Initialize a Git repository with
git init.- Tell Git who you are with
git config.- Stage a file with
git addand save a snapshot withgit commit.Prerequisites: Lesson 01 (why Git exists, save points for files) | Previous << 01 | Next 03 >>
From folder to time machine
In the last lesson, a repository was a folder Git is watching. This lesson makes that real. You will create a folder, turn it into a repository, and save your first snapshot.
Git keeps its history in a hidden .git directory inside your folder. You do not need to open it; you just need to know it exists. When you run git init, Git creates that directory and the folder becomes a repository 1.
Before you can commit, Git needs to know your name and email address. It writes this information into every commit so you know who made each change later 2.
The three-area model, simplified
Git thinks of your files in three places at once:
- Working directory — the files you see and edit in your folder.
- Staging area — a temporary holding spot for changes you want to commit next.
- Repository — the permanent history of commits 3.
The command git add <file> copies a file from the working directory into the staging area. The command git commit takes everything in the staging area and records it as a new commit 2.
The diagram shows the direction of the two main commands: git add moves changes into the staging area, and git commit moves them into the history.
Worked example (follow along)
Open your terminal and run these commands exactly. They will create a safe practice folder on your Desktop.
If everything worked, the last line should show something like 1 file changed, 1 insertion(+) and a commit hash. That hash is the unique ID of your new commit.
Your turn (faded example)
Here is the same sequence with two blanks. Fill them in with the commands you just learned.
Answer:
Summary + what's next
You turned a folder into a repository, set your name and email, and saved your first commit. You now know the difference between the working directory, staging area, and repository history. In the next lesson, you will read that history and recover an older version of a file.
Footnotes
-
Pro Git, Getting a Git Repository — https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository ↩
-
Pro Git, Recording Changes to the Repository — https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository ↩ ↩2
-
Pro Git, What is Git? — https://git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F ↩