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 are covered in other parts of the track.

Learning path

The course is split into two chapters. Follow the pages in order: each one builds on the previous one and ends with a hands-on challenge.

Chapter Page Main commands
1. Introduction to Git 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
2. Version 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

Practice environment

All challenges 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 challenges set a name and email inside each practice repository only (git config user.name ... without --global), so your global configuration is left untouched.

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 course screenshots show a branch called main. Without configuration, current Git versions name the first branch master. The challenges use git init -b main so the names match. See Creating repositories.

Command summary

Chapter 1: Introduction to Git

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

Chapter 2: Version history

Command Purpose
git log Show commits, from newest to oldest
git log -3 Show only the 3 most recent commits
git log report.md Show only the commits that changed report.md
git log --since='Apr 2 2024' --until='Apr 11 2024' Show commits in a date range
git show c27fa856 Show one commit: its metadata and its changes
git diff Show unstaged changes (working directory vs staging area)
git diff report.md Same, restricted to one file
git diff --staged Show staged changes (staging area vs latest commit)
git diff --staged report.md Same, restricted to one file
git diff 35f4b4d 186398f Show changes between two commits (older first)
git diff HEAD~1 HEAD Show changes between the second most recent and 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 -- report.md Restore one file as it was in an earlier commit
git restore --staged report.md Remove one file from the staging area
git restore --staged . Remove all files from the staging area

Beyond this part

The course ends by pointing to the next topics in the Git learning path: branches, remote repositories, rebasing, bisecting, and submodules. They are not covered in this part.