Git: Version Control and Teamwork
Exit

Lesson 1 of 9 · Fundamentals

Why a version control system? Installing and configuring Git

35 min Node.js 22 (Debian, git, npm, python3)

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.name and user.email: they are recorded in every commit.
  • git config reads and writes the configuration at three levels: --system (the whole machine), --global (your account), --local (a single repository). The closest level wins.
  • git init turns a folder into a repository: it creates a hidden .git subfolder 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.html to 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.autocrlf from the start. You will see in level 3 that a .gitattributes file 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.email in every new repository if you have several accounts. An address that matches no GitHub account produces "orphan" commits.
  • git init in the wrong folder (your entire home folder, for example): git status then shows thousands of files. Simply delete the .git created by mistake with rm -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 commit opens the configured editor. If it is vim and you are lost, press Esc, then type :q! and press Enter to cancel.

Lab

In this lab (a Debian container with Git), you start from a machine where Git has never been configured:

  1. you save your global identity;
  2. you set the default branch to main;
  3. you create the /root/vitrine repository with git init;
  4. you make your first commit in it with an index.html file.

Check your work with git config --list, git status and git log --oneline.

Quiz

  1. Name two concrete problems that a version control system solves in a team.
  2. What does "Git is distributed" mean?
  3. What is the difference between git config --global and git config --local? Which one wins?
  4. What does the .git folder created by git init contain?
  5. Why must you set user.email before the first commit?
Answers
  1. 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.
  2. Every copy of the repository contains the whole history: you can commit, read the log or create branches without a server or a network.
  3. --global applies to all your repositories (the ~/.gitconfig file); --local applies to a single repository (.git/config). The local setting wins, because it is more specific.
  4. The whole repository: the commit history, the branches, the tags and the local configuration. Deleting it means losing the history.
  5. 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.

Practical exercises

To do on Node.js 22 (Debian, git, npm, python3)

The online terminal is coming soon. Meanwhile, do these tasks on your own Linux machine, a virtual machine or WSL.

  1. Save your Git identity for the whole root account: a name (user.name) and an email address (user.email) containing an @. Example: Salma Bennani, salma@agence-atlas.ma.
    Show a hintgit config --global user.name "Your Name" then git config --global user.email you@example.ma
    Show the solution
    git config --global user.name "Salma Bennani" && git config --global user.email salma@agence-atlas.ma

    Check : test -n "$(git config --global user.name)" && git config --global user.email | grep -q @

  2. Configure Git so that every new repository starts on a branch named main (option init.defaultBranch).
    Show a hintgit config --global init.defaultBranch main
    Show the solution
    git config --global init.defaultBranch main

    Check : test "$(git config --global init.defaultBranch)" = main

  3. Create the folder /root/vitrine and turn it into a Git repository.
    Show a hintmkdir -p /root/vitrine then git init /root/vitrine (or cd /root/vitrine && git init)
    Show the solution
    mkdir -p /root/vitrine && git init -q /root/vitrine

    Check : test -d /root/vitrine/.git && git -C /root/vitrine rev-parse --is-inside-work-tree >/dev/null 2>&1

  4. In /root/vitrine, create an index.html file (one line is enough) and save it in a first commit. The commit must be on the main branch.
    Show a hintcd /root/vitrine ; echo '<h1>Atlas Web</h1>' > index.html ; git add index.html ; git commit -m "Premier commit"
    Show the solution
    cd /root/vitrine && echo "<h1>Atlas Web</h1>" > index.html && git add index.html && git commit -qm "Premier commit : page d accueil"

    Check : git -C /root/vitrine log --oneline 2>/dev/null | grep -q . && git -C /root/vitrine ls-files | grep -qx index.html && test "$(git -C /root/vitrine branch --show-current)" = main

Official references consulted: Git — git (page principale du manuel) — https://git-scm.com/docs/git ; Git — git-config — https://git-scm.com/docs/git-config ; Git — git-init — https://git-scm.com/docs/git-init ; Git — gittutorial — https://git-scm.com/docs/gittutorial. Original MasterLinux text.