Aller au contenu

Introduction to Git

This part covers the fundamentals of Git as a local version control system: what version control is, how to create a repository, how to record changes, and how to inspect, compare, and undo them. Everything here happens on your own machine. Remote repositories and GitHub itself come later.

Learning path

Follow the pages in order: each one builds on the previous one and ends with hands-on labs.

Topic Page Main commands
Getting started Version control and Git basics pwd, ls, cd, git --version
Creating repositories git init, git status
Staging and committing files git add, git commit -m
Working with history Viewing the version history git log, git show
Comparing versions git diff, git diff --staged
Restoring and reverting files git revert, git checkout -- <file>, git restore --staged

The big picture

Every page in this part is about the same three places. Changes move forward with git add and git commit; the other commands inspect the history or bring an earlier state back:

flowchart LR
    wd["Working directory<br/>files you edit"]
    idx["Staging area<br/>the next commit, being prepared"]
    repo[("Repository<br/>recorded history in .git/")]

    wd -- "git add" --> idx
    idx -- "git commit" --> repo
    repo -. "git log, git show, git diff<br/>git revert, git checkout, git restore" .-> wd

The example project

The examples follow a small project called homelab: the notes and hardware inventory of a few home servers (a DNS server, a NAS, a web server...). It contains three files:

File Content
README.md A short description and a to-do list
inventory.csv One line per machine: hostname,ip,role
services.md Which service runs on which machine

All outputs shown in these notes come from real Git sessions on this project.

Practice environment

All labs run on Linux in a dedicated practice folder, ~/git-practice, which you can delete at any time. They never touch your real repositories.

You need Git installed:

git --version

If the command is not found, install Git with your distribution's package manager (for example sudo apt install git on Debian/Ubuntu, or sudo dnf install git on Fedora).

Git refuses to commit if it does not know who you are. The labs set a name and email inside each practice repository only (git config user.name ... without --global), so your global configuration is left untouched.

How the labs work

Every page ends with three labs:

Lab Purpose
Lab 1 Guided: the main commands of the page, step by step.
Lab 2 The same commands in another situation, often with a mistake to make on purpose, or a change of mind to recover from.
Lab 3 More autonomous: a scenario to solve, or a role to play.

Each lab states its objective and initial state, gives a setup script, lists the tasks, and describes the expected result so you can check your work. The solution is folded: try first, then compare. The last page of the part ends with a final lab that uses every command of the part, with fewer hints.

Output language and default branch

The outputs shown in these notes are in English. If your system language is different, Git may print translated messages. Run a command with LC_ALL=C in front of it (for example LC_ALL=C git status) to get English output.

The examples use a first branch called main. Depending on your configuration, Git may call it master instead; the labs use git init -b main so that the names always match. See Creating repositories.

Command summary

Getting started

Command Purpose
pwd Print the current working directory
ls List the contents of the current directory
cd <directory> Change the current directory
git --version Display the installed Git version
git init <name> Create a new directory <name> and initialize a repository in it
git init Turn the current directory into a Git repository
git status Show the state of the working directory and the staging area
git add <file> Add one file to the staging area
git add . Add all changed files in the current directory and its subdirectories
git commit -m "message" Record the staged changes as a new commit, with a log message

Working with history

Command Purpose
git log Show commits, from newest to oldest
git log -3 Show only the 3 most recent commits
git log inventory.csv Show only the commits that changed inventory.csv
git log --since='2026-03-05' --until='2026-03-20' Show commits in a date range
git show 420feb79 Show one commit: its metadata and its changes
git diff Show unstaged changes (working directory vs staging area)
git diff README.md Same, restricted to one file
git diff --staged Show staged changes (staging area vs latest commit)
git diff --staged README.md Same, restricted to one file
git diff 2553c7d dd032b5 Show changes between two commits (older first)
git diff HEAD~1 HEAD Show the changes introduced by the most recent commit
git revert HEAD Create a new commit that undoes the latest commit
git revert --no-edit HEAD Same, without opening the text editor
git revert -n HEAD Undo the changes in the files and staging area, without committing
git checkout HEAD~1 -- README.md Restore one file as it was in an earlier commit
git restore --staged README.md Remove one file from the staging area
git restore --staged . Remove all files from the staging area

What comes next

Once these basics are solid, the next step is Intermediate Git: branches, merging, and remote repositories. Further topics include rebasing, bisecting (finding the commit that introduced a bug), and submodules.