Aller au contenu principal

Exercices Pratiques Git


Table des matières


  1. Exercice 1 : Workflow complet
  2. Exercice 2 : Gestion des branches
  3. Exercice 3 : Résolution de conflits
  4. Exercice 4 : Rebase interactif
  5. Exercice 5 : Scénario réaliste
  6. Solutions


1 - Exercice 1 : Workflow complet



Objectif

Créer un projet, le pousser sur GitHub, et faire plusieurs commits.

Instructions

  1. Créez un nouveau dossier git-exercice-1
  2. Initialisez Git
  3. Créez les fichiers suivants :
    • index.html avec une structure HTML de base
    • style.css avec quelques styles
    • README.md avec une description du projet
  4. Faites 3 commits séparés (un pour chaque fichier)
  5. Créez un dépôt sur GitHub
  6. Poussez votre code

Vérification

# Vous devez voir 3 commits
git log --oneline
# abc1234 docs: add README
# def5678 style: add CSS
# ghi9012 feat: add HTML structure
💡 Indices
mkdir git-exercice-1
cd git-exercice-1
git init
# Créer les fichiers...
git add index.html
git commit -m "feat: add HTML structure"
# etc.

🔝 Retour à la table des matières



2 - Exercice 2 : Gestion des branches



Objectif

Travailler avec plusieurs branches et les fusionner.

Instructions

  1. Partez du projet de l'exercice 1
  2. Créez une branche feature/contact-page
  3. Sur cette branche, créez contact.html
  4. Faites un commit
  5. Revenez sur main
  6. Créez une branche feature/about-page
  7. Sur cette branche, créez about.html
  8. Faites un commit
  9. Revenez sur main
  10. Mergez les deux branches dans main
  11. Supprimez les branches de feature

Vérification

git log --oneline --graph --all
# Doit montrer les merges ou un historique linéaire

Questions bonus

  • Quelle différence si vous utilisez --no-ff pour le merge ?
  • Comment faire un merge squash ?
💡 Indices
git checkout -b feature/contact-page
# Créer contact.html
git add contact.html
git commit -m "feat: add contact page"
git checkout main
# Répéter pour about
git merge feature/contact-page
git merge feature/about-page
git branch -d feature/contact-page
git branch -d feature/about-page

🔝 Retour à la table des matières



3 - Exercice 3 : Résolution de conflits



Objectif

Créer et résoudre un conflit de merge.

Mise en place

mkdir git-exercice-3
cd git-exercice-3
git init

# Fichier initial
echo "Ligne 1" > file.txt
echo "Ligne 2" >> file.txt
echo "Ligne 3" >> file.txt
git add file.txt
git commit -m "Initial commit"

Instructions

  1. Créez une branche feature/modification-a
  2. Modifiez la ligne 2 de file.txt : "Ligne 2 - Version A"
  3. Commitez
  4. Revenez sur main
  5. Créez une branche feature/modification-b
  6. Modifiez la ligne 2 de file.txt : "Ligne 2 - Version B"
  7. Commitez
  8. Revenez sur main
  9. Mergez feature/modification-a
  10. Mergez feature/modification-b (conflit !)
  11. Résolvez le conflit en gardant les deux versions

Résultat attendu

file.txt après résolution :

Ligne 1
Ligne 2 - Version A et Version B
Ligne 3
💡 Indices

Pour résoudre le conflit :

  1. Ouvrez le fichier
  2. Vous verrez <<<<<<, =======, >>>>>>>
  3. Éditez pour garder ce que vous voulez
  4. git add file.txt
  5. git commit

🔝 Retour à la table des matières



4 - Exercice 4 : Rebase interactif



Objectif

Nettoyer l'historique avec rebase interactif.

Mise en place

mkdir git-exercice-4
cd git-exercice-4
git init

echo "v1" > file.txt && git add . && git commit -m "Initial"
echo "v2" >> file.txt && git commit -am "wip"
echo "v3" >> file.txt && git commit -am "more wip"
echo "v4" >> file.txt && git commit -am "almost done"
echo "v5" >> file.txt && git commit -am "fix typo"
echo "v6" >> file.txt && git commit -am "final"

Instructions

Utilisez git rebase -i HEAD~5 pour :

  1. Squash "wip", "more wip", "almost done" en un seul commit "feat: implement feature"
  2. Squash "fix typo" avec "final" en "feat: finalize feature"

Résultat attendu

git log --oneline
# abc1234 feat: finalize feature
# def5678 feat: implement feature
# ghi9012 Initial
💡 Indices

Dans l'éditeur du rebase interactif :

pick ghi9012 wip
squash xxx more wip
squash xxx almost done
pick xxx fix typo
squash xxx final

Puis modifiez les messages.

🔝 Retour à la table des matières



5 - Exercice 5 : Scénario réaliste



Contexte

Vous travaillez sur un site web avec une équipe. Simulez le workflow complet.

Instructions

Partie A : Setup

  1. Créez un dépôt website-team
  2. Créez une structure de projet :
website-team/
├── index.html
├── css/
│ └── style.css
├── js/
│ └── app.js
└── README.md
  1. Commitez et poussez sur GitHub

Partie B : Feature development

  1. Créez une branche feature/navbar
  2. Ajoutez une navbar dans index.html
  3. Ajoutez les styles correspondants
  4. Faites 2-3 commits
  5. Poussez la branche

Partie C : Hotfix

Pendant ce temps, un bug urgent :

  1. Depuis main, créez hotfix/critical-bug
  2. "Corrigez" le bug (modifiez un fichier)
  3. Mergez dans main
  4. Poussez

Partie D : Mise à jour et finalisation

  1. Retournez sur feature/navbar
  2. Rebasez sur le nouveau main
  3. Résolvez les conflits éventuels
  4. Squash vos commits en un seul
  5. Mergez dans main avec --no-ff
  6. Créez un tag v1.0.0
  7. Poussez tout

Vérification finale

git log --oneline --graph --all

Doit montrer :

  • Le hotfix mergé
  • La feature mergée avec un merge commit
  • Le tag v1.0.0

🔝 Retour à la table des matières



6 - Solutions



Solution Exercice 1

mkdir git-exercice-1
cd git-exercice-1
git init

cat > index.html << 'EOF'
<!DOCTYPE html>
<html>
<head><title>Mon Site</title></head>
<body><h1>Hello</h1></body>
</html>
EOF
git add index.html
git commit -m "feat: add HTML structure"

cat > style.css << 'EOF'
body { font-family: Arial; }
h1 { color: blue; }
EOF
git add style.css
git commit -m "style: add CSS"

echo "# Mon Projet" > README.md
git add README.md
git commit -m "docs: add README"

# Sur GitHub, créez le repo puis :
git remote add origin [email protected]:user/git-exercice-1.git
git push -u origin main

Solution Exercice 3

# Après le conflit
# file.txt contient :
<<<<<<< HEAD
Ligne 2 - Version A
=======
Ligne 2 - Version B
>>>>>>> feature/modification-b

# Éditer pour :
Ligne 2 - Version A et Version B

git add file.txt
git commit -m "merge: combine modifications A and B"

Solution Exercice 4

Dans le rebase interactif, configurez :

pick abc Initial
pick def wip
squash xxx more wip
squash xxx almost done
reword xxx fix typo
squash xxx final

Points clés à retenir

ConceptCommande clé
Workflow de baseadd → commit → push
Branchescheckout -b, merge, branch -d
ConflitsÉditer, add, commit
Nettoyagerebase -i, squash
Versioningtag -a v1.0.0

🎉 Félicitations !

Vous avez terminé le cours Git ! Vous maîtrisez maintenant :

  • ✅ Les commandes de base
  • ✅ Le workflow Git
  • ✅ Les branches et le merge
  • ✅ Le rebase et le squash
  • ✅ La résolution de conflits
  • ✅ La synchronisation avec GitHub
  • ✅ Les tags et versions

Prochaines étapes

  1. Pratiquez quotidiennement
  2. Contribuez à des projets open source
  3. Explorez les fonctionnalités avancées (hooks, submodules, etc.)

🔝 Retour à la table des matières