Local Repository and Commits

7 min

Table of contents


1 — The three Git zones

To understand commits, you first need to picture the three zones through which Git moves your files.

ZoneDescriptionCommand to enter it
Working directoryYour files as you edit them(direct editing)
Staging area (index)The changes you prepare to recordgit add
Local repositoryThe permanent commit historygit 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.

↑ Back to top


2 — Initialize a local repository

To turn an ordinary folder into a Git repository:

bash
# Go into the project folder
cd mon-projet

# Initialize the repository
git init

This creates a hidden .git/ subdirectory that contains the entire history and configuration of the repository.

bash
# Check the state of the freshly created repository
git status

Never 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.

✅ See a solution
bash
git init
git status

↑ Back to top


3 — Tracking files

A file can be tracked or untracked by Git.

bash
# Stage a specific file
git add fichier.txt

# Stage every change in the folder
git add .

# See the status (tracked / untracked / staged)
git status
CommandEffect
git add fichier.txtStages a specific file
git add .Stages every modified/new file
git restore --staged fichier.txtRemoves a file from the staging area
git statusDisplays the status of each file

git status is your best friend: use it before and after every git add to 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.

✅ See a solution
bash
git add index.html

↑ Back to top


4 — Create a commit

A commit is a snapshot of your project at a given moment, together with a message that explains the change.

bash
# Stage then record
git add .
git commit -m "Add the home page"

Each commit has:

ElementDescription
An identifier (hash)E.g. a1b2c3d… — unique
An authorYour name + email (configuration from lesson 03)
A dateTimestamp of the commit
A messageThe description of the change
A parentThe previous commit (history chain)
bash
# See the commit history
git log
git log --oneline   # compact version

A 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”.

✅ See a solution
bash
git add .
git commit -m "Add the contact page"

↑ Back to top


5 — Good message practices

The commit message tells the story of the project. A good message saves hours for the whole team (and for yourself in 6 months).

Basic rules

  • Write in the present imperative: “Add…”, “Fix…”, “Remove…”.
  • One short summary line (≤ 50 characters), then a detailed body if needed.
  • Explain the why, not only the what.

Comparison

Bad messageGood message
updateUpdate the Maven dependency to 3.9
fix bugFix the startup crash when config is missing
wipAdd validation for the login form

Common convention (Conventional Commits)

feat: add token authentication
fix: fix result pagination
docs: complete the installation README

Mnemonic: a good message should complete the sentence “If I apply this commit, it will…”. Example: “…add the home page”.

↑ Back to top


6 — The .gitignore file

Some files must never be versioned: temporary files, bulky dependencies, secrets, compiled files. The .gitignore file tells Git to ignore them.

bash
# Example contents of a .gitignore file
target/
node_modules/
*.log
.env
.DS_Store
IgnoreWhy
node_modules/, target/Rebuilt automatically, bulky
.env, *.keySecrets — never in Git!
*.log, *.tmpTemporary 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 .env in .gitignore from the start.

🔧 Mini-exercise — Write the line to add to a .gitignore to stop Git from versioning the secrets file .env.

✅ See a solution
.env

↑ Back to top


7 — Quiz — Local repository and commits

Question 1: Which command turns an ordinary folder into a Git repository?

a) git start

b) git init

c) git new

d) git create

See the solution

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

See the solution

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

See the solution

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) .

See the solution

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

See the solution

Answer: b).gitignore lists the files Git must ignore, especially secrets (.env) and rebuildable folders (node_modules/).

↑ Back to top


8 — Practice — Your first repository

Instructions

Create a local repository, add a file, ignore a secret file, and make two clean commits.


Correction — Expected command sequence

bash
# 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 --oneline

Expected result:

b2c3d4e Complete the description in the README
a1b2c3d Initialize the project with README and gitignore

Check that .env never appears in git status: that is the proof your .gitignore works and your secret is protected.

↑ Back to top


9 — Summary

Key takeaways

  1. Three zones: working directory → staging (git add) → local repository (git commit).
  2. git init creates the repository (.git/ folder).
  3. A commit is a snapshot + a message, linked to the parent commit.
  4. Good messages: imperative, clear, atomic, explaining the why.
  5. .gitignore protects secrets and excludes useless files.

What's next

Lesson 07 — Branches and history: work on several versions in parallel without breaking the main version.

↑ Back to top


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