Show contents
Chapter 1 of 7 · 20 min
Your first Git repository
By the end of this chapter, you'll be able to
- Explain what a version control system is for
- Configure Git with your name and email address
- Create a repository and record a first commit in it
- Tell apart the working directory, the staging area and the repository
A camera for your project
A version control system records snapshots of your project. Each snapshot, called a commit, holds the state of every file at a given moment, with a message explaining what changed, the author and the date. You can then:
- go back to any past version;
- see exactly what changed between two versions, line by line;
- work on an idea on the side without breaking the version that works;
- bring together the work of several people.
Git is distributed: every copy of the project contains the whole history. You can work offline and synchronise with a server later. Hosting platforms such as GitHub, GitLab or Codeberg are servers that host Git repositories, with collaboration tools on top. Git itself is free software that works without them.
Install Git and introduce yourself
On Windows, install “Git for Windows” from the official site git-scm.com. On macOS, the git --version command offers to install it. On Linux, use the package manager (for example sudo apt install git). You then type the commands in a terminal: the Terminal application on macOS and Linux, “Git Bash” on Windows.
Before your first commit, tell Git who you are. This information is written into every commit:
git config --global user.name "Awa Diallo"
git config --global user.email "awa@example.com"
git config --global init.defaultBranch main
git config --global user.name
Awa Diallo
--global saves the setting for all your projects. The third line names the main branch of new repositories main (you will see branches later). The last line, without a value, displays the current setting.
Create a repository
A repository is a folder tracked by Git. Go to your project's folder, then:
git init
Git answers with a line such as Initialized empty Git repository in /home/awa/project/.git/. It has created a hidden .git subfolder: this is where it stores the whole history. Never edit it by hand, and do not delete it: you would lose the history.
Git's three areas
Let's create a home page for a bakery's website, then ask Git about the state of the repository:
echo "<h1>Kofi's Bakery</h1>" > index.html
git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
nothing added to commit but untracked files present (use "git add" to track)
Git sees index.html but does not track it yet (untracked). A file goes through three areas:
- the working directory: your files, as you edit them;
- the staging area (or index): what you have chosen to put into the next commit;
- the repository: the history of commits, stored in
.git.
The snapshot is in the history. The working directory matches the last commit.
Text description of the animation
- You create index.html. Git sees it but does not track it yet: git status -s shows ??.
- git add copies the current state of the file into the staging area.
- The file is ready for the next commit: A for “added”.
- git commit records the whole content of the staging area, with your message.
- The snapshot is in the history. The working directory matches the last commit.
Let's stage the file, then check with the short version of status:
git add index.html
git status -s
A index.html
The first column describes the staging area, the second the working directory: A means “added to the next commit”. Let's take the snapshot:
git commit -m "Add the home page"
Git prints a summary that looks like this:
[main (root-commit) 3f2a9c1] Add the home page
1 file changed, 1 insertion(+)
create mode 100644 index.html
3f2a9c1 is the beginning of the commit's identifier, a fingerprint computed from its content, its author and its date: yours will be different. root-commit marks the very first commit of the repository. Let's display the history, with the author and message of each commit:
git log --format="%an: %s"
Awa Diallo: Add the home page
Your turn
Mini-lab: your first repositoryGit simulator
Terminal
Click “Run”: the commands run in a fresh repository. Type “help” for the list of commands.Teaching simulator: it reproduces the essential Git commands in your browser, with nothing to install · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
This lab is a simulator: it reproduces Git's essential commands in your browser, with nothing to install. Add lines, change the messages, type help to see the available commands.
Worked exercise
Worked exercise: record the opening hours
In the bakery's repository, the file hours.txt already exists but is not tracked. Record it in a commit titled “Add the opening hours”, then display the messages of all the commits, from the most recent to the oldest.
Hint 1
Hint 2
git log --format="%s" displays only the message (subject) of each commit.Show the solution and common mistakes
git add hours.txt
git commit -m "Add the opening hours"
git log --format="%s"
Add the opening hours
Add the home page
git log always shows the most recent commit first.
Common mistakes:
- Forgetting the quotes around the message:
git commit -m Add the hoursonly takes “Add” as the message and treats the other words as file names. - Running
git inita second time “to be sure”: it is harmless, but useless. The repository already exists.
Summary
- 1Git records snapshots of your project, called commits, with a message, an author and a date.
- 2git config --global user.name and user.email introduce you once and for all.
- 3git init creates a repository: the history is stored in the hidden .git folder.
- 4A file goes from the working directory to the staging area (git add), then to the repository (git commit).
- 5git status tells you where your files stand at any time: check it before every commit.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.