To understand commits, you first need to picture the three zones through which Git moves your files.
| Zone | Description | Command to enter it |
|---|---|---|
| Working directory | Your files as you edit them | (direct editing) |
| Staging area (index) | The changes you prepare to record | git add |
| Local repository | The permanent commit history | git commit |
Analogy: packing a parcel. The working directory is your messy desk; the staging area is the box where you put what you want to send; the commit is the moment you seal the box and archive it.
To turn an ordinary folder into a Git repository:
# Go into the project folder
cd mon-projet
# Initialize the repository
git initThis creates a hidden .git/ subdirectory that contains the entire history and configuration of the repository.
# Check the state of the freshly created repository
git statusNever delete the
.git/folder: it contains the entire history. Deleting it means losing versioning (but not your current files).
🔧 Mini-exercise — Write the command that turns the current folder into a Git repository, then the one that displays its status.
git init
git statusA file can be tracked or untracked by Git.
# Stage a specific file
git add fichier.txt
# Stage every change in the folder
git add .
# See the status (tracked / untracked / staged)
git status| Command | Effect |
|---|---|
git add fichier.txt | Stages a specific file |
git add . | Stages every modified/new file |
git restore --staged fichier.txt | Removes a file from the staging area |
git status | Displays the status of each file |
git statusis your best friend: use it before and after everygit addto see exactly what Git is about to record.
🔧 Mini-exercise — You just modified index.html and style.css. Write the command that stages only index.html.
git add index.htmlA commit is a snapshot of your project at a given moment, together with a message that explains the change.
# Stage then record
git add .
git commit -m "Add the home page"Each commit has:
| Element | Description |
|---|---|
| An identifier (hash) | E.g. a1b2c3d… — unique |
| An author | Your name + email (configuration from lesson 03) |
| A date | Timestamp of the commit |
| A message | The description of the change |
| A parent | The previous commit (history chain) |
# See the commit history
git log
git log --oneline # compact versionA good commit is atomic: it does one coherent thing. Avoid the catch-all “lots of stuff” commit — prefer several small, clear commits.
🔧 Mini-exercise — Stage all your changes and create a commit whose message is “Add the contact page”.
git add .
git commit -m "Add the contact page"The commit message tells the story of the project. A good message saves hours for the whole team (and for yourself in 6 months).
| Bad message | Good message |
|---|---|
update | Update the Maven dependency to 3.9 |
fix bug | Fix the startup crash when config is missing |
wip | Add validation for the login form |
feat: add token authentication
fix: fix result pagination
docs: complete the installation READMEMnemonic: a good message should complete the sentence “If I apply this commit, it will…”. Example: “…add the home page”.
Some files must never be versioned: temporary files, bulky dependencies, secrets, compiled files. The .gitignore file tells Git to ignore them.
# Example contents of a .gitignore file
target/
node_modules/
*.log
.env
.DS_Store| Ignore | Why |
|---|---|
node_modules/, target/ | Rebuilt automatically, bulky |
.env, *.key | Secrets — never in Git! |
*.log, *.tmp | Temporary files with no value |
Golden security rule: never commit passwords, API keys, or secrets. Once they are in the Git history, a secret stays there — even if deleted later. Put
.envin.gitignorefrom the start.
🔧 Mini-exercise — Write the line to add to a .gitignore to stop Git from versioning the secrets file .env.
.envQuestion 1: Which command turns an ordinary folder into a Git repository?
a) git start
b) git init
c) git new
d) git create
Answer: b) — git init creates the .git/ subdirectory that contains the history and makes the folder a repository.
Question 2: What is the correct order of the three Git zones?
a) Repository → staging → working directory
b) Working directory → staging → local repository
c) Staging → repository → working directory
d) Working directory → repository → staging
Answer: b) — You edit (working directory), you prepare with git add (staging), then you record with git commit (local repository).
Question 3: What is git add for?
a) Creating a commit
b) Preparing changes in the staging area
c) Deleting a file
d) Sending the code to GitHub
Answer: b) — git add moves changes from the working directory to the staging area, before the commit.
Question 4: Which one is a good commit message?
a) truc
b) wip
c) Fix the startup crash when the config is missing
d) .
Answer: c) — It is clear, in the imperative, and explains the change. The others are vague and useless in the history.
Question 5: Why use a .gitignore?
a) To speed up the computer
b) To stop Git from versioning certain files (temporary files, secrets, dependencies)
c) To delete the history
d) To ignore commits
Answer: b) — .gitignore lists the files Git must ignore, especially secrets (.env) and rebuildable folders (node_modules/).
Create a local repository, add a file, ignore a secret file, and make two clean commits.
# 1. Create and enter the folder
mkdir mon-premier-depot
cd mon-premier-depot
# 2. Initialize the repository
git init
# 3. Create a content file and a .gitignore
echo "# Mon projet" > README.md
echo ".env" > .gitignore
echo "SECRET=123" > .env # this file must be ignored
# 4. Check the status (.env must NOT appear)
git status
# 5. First commit
git add README.md .gitignore
git commit -m "Initialize the project with README and gitignore"
# 6. Modify then second commit
echo "Description du projet" >> README.md
git add README.md
git commit -m "Complete the description in the README"
# 7. Consult the history
git log --onelineExpected result:
b2c3d4e Complete the description in the README
a1b2c3d Initialize the project with README and gitignoreCheck that
.envnever appears ingit status: that is the proof your.gitignoreworks and your secret is protected.
git add) → local repository (git commit).git init creates the repository (.git/ folder)..gitignore protects secrets and excludes useless files.Lesson 07 — Branches and history: work on several versions in parallel without breaking the main version.
All rights reserved. Any reproduction, distribution, use or adaptation of this course, in whole or in part, is strictly prohibited without the prior written authorization of Dr. Haythem REHOUMA.
Course created by Dr. Haythem REHOUMA — Development and Deployment of Data Solutions