Who has never had a folder full of rapport_v2.docx, rapport_final.docx, rapport_final_VRAI.docx? Git solves this problem: it is a version control system that records the complete history of a project. You can go back in time, understand who changed what and why, and work with others without overwriting their work. Git is now the standard tool, from showcase websites to data science projects.
Configure Git (just once)
git --version # checks the installation
git config --global user.name "Léa Martin"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main # name of the initial branch
The name and email are written into every commit: they identify the author.
Create a repository
mkdir mon-projet && cd mon-projet
git init # creates the repository: a hidden .git folder containing the whole history
git status # the command to type without moderation
Never delete the .git folder: it is what holds the history.
The three areas
This is the central concept in Git:
- the working directory: your files as you edit them;
- the staging area (or index): what you have chosen to include in the next snapshot;
- the history: the sequence of recorded commits.
modified files --git add--> staging area --git commit--> history
A commit is a snapshot of the project at a given moment, along with a message, an author, a date and a unique identifier (a hash such as a1b2c3d).
The basic cycle
echo "# My project" > README.md
git status # README.md appears under "Untracked files"
git add README.md # we place it in the staging area
git status # it appears under "Changes to be committed"
git commit -m "Add the README" # we record the snapshot
Then, with each change:
# ... you edit index.html and style.css ...
git status # "modified" files
git add index.html style.css # or git add . for the whole current directory
git commit -m "Add the home page and its styling"
Why an intermediate step? Because it lets you choose: if you have fixed a bug and started a new feature, you can record them in two separate commits. git add -p even lets you select pieces of a file.
Good commits
- Small and coherent: one commit = one logical change.
- Well named: a short message in the imperative that says what the commit does (“Fix the VAT calculation”), not “changes” or “wip”.
- Frequent: committing often means multiplying the possible points to return to.
Get into the habit of running git status before every add and every commit: Git always tells you where you stand and often suggests the next command.
Exercises
Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.
Configure Git and create a repository
Configure your name and email, then create a folder
atelier-git, initialize a repository in it and check its state.- git config --global user.name displays your name
- git config --global user.email displays your email
- The atelier-git folder contains a hidden .git subfolder (visible with ls -a)
- git status reports "No commits yet"
Two separate commits
In
atelier-git, createREADME.mdandnotes.txt. Make a first commit containing only the README, then a second one containing notes.txt.- Before the first commit, git status shows README.md under "Changes to be committed" and notes.txt under "Untracked files"
- git log --oneline displays exactly 2 commits
- git show --stat HEAD~1 mentions only README.md
- git status reports "nothing to commit, working tree clean"
The role of git add
You modified
app.py, then rangit commit -m "Corrige"withoutgit add. What happens?- The commit contains app.py
- Git reports that nothing is staged for commit
- Git deletes the changes
End-of-lesson quiz
What does git init do?
- Sends the project to GitHub
- Creates a Git repository in the current directory
- Records a commit
In what order do you record a change?
- git commit then git add
- git add then git commit
- git push then git commit
Which commit message is best?
- modifs
- Corrige le calcul du total du panier
- commit du 10/11
The rest of the course
- Explore the history and undo changes15 min
- Branches, merges and conflicts20 min
- GitHub: working with a remote repository20 min
- Collaborating: .gitignore, pull requests and review20 min