Lesson 1 of 9 · Fundamentals
Why a version control system? Installing and configuring Git
Objective
Understand the problem that a version control system solves. Install Git on your workstation and configure it correctly (identity, default branch, editor) so that your first commits are clean from the start.
Key points
- A version control system keeps the history of every change to a project: who, what, when, why. You can go back, compare versions and work as a team without overwriting each other's work.
- Git is distributed: each developer has a complete copy of the history and can work offline.
- Before the first commit, set at least
user.nameanduser.email: they are recorded in every commit. git configreads and writes the configuration at three levels:--system(the whole machine),--global(your account),--local(a single repository). The closest level wins.git initturns a folder into a repository: it creates a hidden.gitsubfolder that contains the whole history.
1. The problem: "index-final-v3-OK-really.html"
Imagine the agency Atlas Web, in Casablanca (Maârif district). Four developers are building the website of a chain of pastry shops. Without a tool, this is what happens:
- Youssef sends
index.htmlto Salma on WhatsApp. She edits it while he keeps working on his own copy. In the evening, they have to merge by hand. - The client asks: "put back the banner from the day before yesterday". Nobody remembers which version that was.
- A bug appears in production. Who changed what, and when? Nobody knows.
- The shared folder contains
site-final.zip,site-final-v2.zip,site-final-corrige-ilyas.zip…
A version control system answers all these questions:
| Need | What Git does |
|---|---|
| Go back | every saved state (a commit) can be restored |
| Know who changed what | every commit has an author, a date and a message |
| Work as a team | everyone works on their own branch, then you merge |
| Experiment safely | a test branch can be thrown away if the idea does not work |
| Back up | every copy of the repository (workstation, GitHub or GitLab server) contains the whole history |
Centralized or distributed
Older tools (CVS, Subversion) were centralized: the history lived on a single server, and without a network you could not save anything. Git, created in 2005 by Linus Torvalds to develop the Linux kernel, is distributed: your computer holds the complete repository. You can make commits on the Casa-Rabat train without a connection and share them later.
Git is not GitHub. Git is the software, free and open source, installed on your workstation. GitHub, GitLab and Bitbucket are services that host Git repositories and add tools around them (code review, issues, CI/CD). You will see them in level 2.
2. Installing Git
Git is very often already installed. Check first:
git --version
# git version 2.47.2
If the command is not found, install Git with your system's package manager:
# Debian, Ubuntu
sudo apt update && sudo apt install git
# Rocky Linux, AlmaLinux, RHEL, Fedora
sudo dnf install git
# macOS (Apple command line tools, or Homebrew)
xcode-select --install
brew install git
On Windows, install "Git for Windows" from the official Git website. It provides git, a Git Bash terminal and a credential manager. You can also work in WSL (Ubuntu inside Windows) and install Git with apt, as on a server.
Finding documentation
git help # the most commonly used commands
git help config # the full manual of a command (same as man git-config)
git config -h # the short summary of the options
The official documentation is also online, command by command, on the Git website.
3. Setting your identity
Every commit records a name and an email address. Without them, Git refuses to commit or makes up an identity from the machine name, which produces unreadable histories ("root@pc-salle3").
git config --global user.name "Salma Bennani"
git config --global user.email "salma@agence-atlas.ma"
Use the address of your GitHub or GitLab account: this is how the site links your commits to your profile.
Check:
git config --global user.name
# Salma Bennani
git config --list --show-origin
# file:/root/.gitconfig user.name=Salma Bennani
# file:/root/.gitconfig user.email=salma@agence-atlas.ma
The three configuration levels
| Option | File | Scope |
|---|---|---|
--system |
/etc/gitconfig |
all users of the machine |
--global |
~/.gitconfig (or ~/.config/git/config) |
your account, all your repositories |
--local (default inside a repository) |
the repository's .git/config |
this repository only |
The most specific level wins. A typical case: a freelancer who works for Atlas Web and for their own clients keeps a personal address at the global level, and sets the agency address only in the agency's repositories:
cd ~/projets/patisserie-atlas
git config user.email "karim@agence-atlas.ma" # --local: this repository only
git config --show-origin user.email
# file:.git/config karim@agence-atlas.ma
The ~/.gitconfig file is a plain text file that you can read and edit:
[user]
name = Salma Bennani
email = salma@agence-atlas.ma
[init]
defaultBranch = main
4. Other useful settings
# Name of the branch created by git init (otherwise "master" on many versions)
git config --global init.defaultBranch main
# Editor used to write long commit messages
git config --global core.editor "nano" # or "vim", or "code --wait" for VS Code
# Colors in the terminal (enabled by default on recent versions)
git config --global color.ui auto
# Line endings: on Windows, convert CRLF/LF automatically
git config --global core.autocrlf true # Windows
git config --global core.autocrlf input # Linux and macOS
# Personal shortcuts (aliases)
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"
To read or remove a setting:
git config --global --get core.editor
git config --global --unset alias.st
Line endings. In a mixed Windows/Linux team, a file can look "entirely modified" just because its line endings changed. Set
core.autocrlffrom the start. You will see in level 3 that a.gitattributesfile lets you enforce the rule for the whole team.
5. Creating your first repository
mkdir -p ~/projets/vitrine
cd ~/projets/vitrine
git init
# Initialized empty Git repository in /root/projets/vitrine/.git/
ls -a
# . .. .git
The .git folder is the repository: history, branches, local configuration. The rest of the folder is your working copy. Never edit .git by hand, and do not delete it: you would lose the whole history.
git init also accepts a path: git init /srv/projets/vitrine creates the folder if needed.
Let's create a first file and save it:
echo '<h1>Pâtisserie Atlas</h1>' > index.html
git status
# On branch main
# No commits yet
# Untracked files:
# index.html
git add index.html # "I want to save this file"
git commit -m "Premier commit : page d'accueil"
# [main (root-commit) 3f2a9c1] Premier commit : page d'accueil
# 1 file changed, 1 insertion(+)
git log --oneline
# 3f2a9c1 (HEAD -> main) Premier commit : page d'accueil
You have just made your first commit. The next lesson explains in detail the three areas of Git (working copy, index, repository) and the add, commit, status, log and diff commands.
6. Basic vocabulary
| Term | Meaning |
|---|---|
| repository | the project tracked by Git, with its whole history (the .git folder) |
| commit | a saved snapshot of the project, with an author, a date and a message |
| hash (or SHA) | the unique identifier of a commit, for example 3f2a9c1… (40 characters, often shortened to 7) |
| branch | a line of development; main is the main branch |
| HEAD | "where you are": the commit (and usually the branch) currently checked out |
| remote | another copy of the repository, often on GitHub or GitLab |
Common pitfalls
- Committing with the wrong identity: check
git config user.emailin every new repository if you have several accounts. An address that matches no GitHub account produces "orphan" commits. git initin the wrong folder (your entire home folder, for example):git statusthen shows thousands of files. Simply delete the.gitcreated by mistake withrm -rf ~/.git, after checking the path.- Confusing Git and GitHub: you can use Git all your life without a GitHub account; a local repository is a complete repository.
- Forgetting
-m: without a message,git commitopens the configured editor. If it isvimand you are lost, pressEsc, then type:q!and pressEnterto cancel.
Lab
In this lab (a Debian container with Git), you start from a machine where Git has never been configured:
- you save your global identity;
- you set the default branch to
main; - you create the
/root/vitrinerepository withgit init; - you make your first commit in it with an
index.htmlfile.
Check your work with git config --list, git status and git log --oneline.
Quiz
- Name two concrete problems that a version control system solves in a team.
- What does "Git is distributed" mean?
- What is the difference between
git config --globalandgit config --local? Which one wins? - What does the
.gitfolder created bygit initcontain? - Why must you set
user.emailbefore the first commit?
Answers
- For example: retrieving an old version of a file, knowing who changed what and when, letting several people work at the same time without overwriting each other's work.
- Every copy of the repository contains the whole history: you can commit, read the log or create branches without a server or a network.
--globalapplies to all your repositories (the~/.gitconfigfile);--localapplies to a single repository (.git/config). The local setting wins, because it is more specific.- The whole repository: the commit history, the branches, the tags and the local configuration. Deleting it means losing the history.
- Because the identity is recorded in every commit and is used to link commits to your GitHub or GitLab account; fixing it afterwards requires rewriting the history.
Reference sources: official Git documentation (git-scm.com). Original MasterLinux text.