Exercise: Using Git Reset
Table of contents
- Objectives
- Setup
- Exercise 1: Reset --soft
- Exercise 2: Reset --mixed
- Exercise 3: Reset --hard
- Exercise 4: Recovery with reflog
- Solutions
1 - Objectives
By the end of this exercise, you will know how to:
- ✅ Use
git reset --softto modify a commit - ✅ Use
git reset --mixedto reorganize the staging - ✅ Use
git reset --hardto discard changes - ✅ Recover lost commits with
git reflog
🔝 Back to table of contents
2 - Setup
Create the test repository
# Créer et entrer dans le dossier
mkdir exercice-reset
cd exercice-reset
git init
# Créer le fichier initial
echo "# Mon Projet" > README.md
git add README.md
git commit -m "Initial commit"
# Ajouter plus de contenu
echo "## Description" >> README.md
git commit -am "docs: add description section"
echo "## Installation" >> README.md
git commit -am "docs: add installation section"
echo "## Usage" >> README.md
git commit -am "docs: add usage section"
# Vérifier l'historique
git log --oneline
You should see 4 commits:
abc1234 docs: add usage section
def5678 docs: add installation section
ghi9012 docs: add description section
jkl3456 Initial commit
🔝 Back to table of contents
3 - Exercise 1: Reset --soft
Scenario
You just made the commit "docs: add usage section" but you want to change the message because it's not descriptive enough.
🎯 Your mission
- Use
git reset --softto undo the last commit - Verify that the changes are still in staging
- Redo the commit with a better message
Instructions
# Étape 1 : Vérifier l'état actuel
git log --oneline
# Étape 2 : Annuler le dernier commit avec --soft
# [ÉCRIVEZ LA COMMANDE ICI]
# Étape 3 : Vérifier que les fichiers sont toujours staged
git status
# Étape 4 : Refaire le commit avec un meilleur message
# [ÉCRIVEZ LA COMMANDE ICI]
# Étape 5 : Vérifier le résultat
git log --oneline
Questions
- After the reset --soft, are the files in staging or in the working directory?
- Has the content of README.md changed?
💡 See the solution
# Étape 2
git reset --soft HEAD~1
# Étape 4
git commit -m "docs: add detailed usage section with examples"
Answers:
- The files are in staging (Changes to be committed)
- No, the content has not changed
🔝 Back to table of contents
4 - Exercise 2: Reset --mixed
Scenario
You have added several files to staging but you want to commit them separately.
🎯 Your mission
- Create two new files and add them to staging
- Use
git resetto remove them from staging - Commit them separately
Instructions
# Étape 1 : Créer deux fichiers
echo "Configuration settings" > config.txt
echo "Test file content" > test.txt
# Étape 2 : Ajouter les deux au staging
git add config.txt test.txt
# Étape 3 : Vérifier le staging
git status
# Étape 4 : Retirer les fichiers du staging avec reset
# [ÉCRIVEZ LA COMMANDE ICI]
# Étape 5 : Vérifier que les fichiers sont unstaged
git status
# Étape 6 : Commiter séparément
git add config.txt
git commit -m "chore: add config file"
git add test.txt
git commit -m "test: add test file"
# Étape 7 : Vérifier l'historique
git log --oneline
Questions
- What is the difference between
git resetandgit reset --mixed? - Are the files removed from the working directory after the reset?
💡 See the solution
# Étape 4
git reset
# ou
git reset --mixed
# ou pour un fichier spécifique
git reset HEAD config.txt
Answers:
- No difference!
--mixedis the default option - No, the files remain in the working directory
🔝 Back to table of contents
5 - Exercise 3: Reset --hard
Scenario
You made changes that don't work. You want to discard everything and return to the clean state.
⚠️ Caution
This exercise will delete data! Make sure you are in the test repository.
🎯 Your mission
- Make "broken" changes
- Use
git reset --hardto discard everything - Verify that everything is back to the clean state
Instructions
# Étape 1 : Faire des modifications "cassées"
echo "BROKEN CODE" > broken.txt
echo "More broken stuff" >> README.md
git add .
git commit -m "wip: broken stuff"
# Vérifier l'état
git log --oneline
cat README.md
# Étape 2 : Annuler complètement avec reset --hard
# [ÉCRIVEZ LA COMMANDE ICI]
# Étape 3 : Vérifier que tout est revenu
git log --oneline
cat README.md
ls # broken.txt ne devrait plus exister
Questions
- After the reset --hard, can you recover the broken.txt file?
- How do you make sure you don't lose important work before a reset --hard?
💡 See the solution
# Étape 2
git reset --hard HEAD~1
Answers:
- Yes, with
git reflogandgit checkout <hash> -- broken.txt - Do a
git stashbeforehand, or create a backup branch
🔝 Back to table of contents
6 - Exercise 4: Recovery with reflog
Scenario
Oops! You did a reset --hard and lost an important commit. Use the reflog to recover it.
🎯 Your mission
- Make an "important" commit
- Delete it with reset --hard
- Recover it with reflog
Instructions
# Étape 1 : Créer un commit "important"
echo "VERY IMPORTANT DATA" > important.txt
git add important.txt
git commit -m "feat: add very important file"
# Noter le hash du commit
git log --oneline -1
# Étape 2 : Le supprimer "par erreur"
git reset --hard HEAD~1
# Vérifier qu'il a disparu
git log --oneline
ls important.txt # Erreur, fichier non trouvé
# Étape 3 : Utiliser reflog pour trouver le commit
# [ÉCRIVEZ LA COMMANDE ICI]
# Étape 4 : Récupérer le commit
# [ÉCRIVEZ LA COMMANDE ICI]
# Étape 5 : Vérifier la récupération
git log --oneline
cat important.txt
💡 See the solution
# Étape 3
git reflog
# Trouver la ligne qui dit "commit: feat: add very important file"
# Exemple: abc1234 HEAD@{1}: commit: feat: add very important file
# Étape 4 - Option A : Reset vers ce commit
git reset --hard abc1234
# Étape 4 - Option B : Créer une branche pour ce commit
git branch recovered abc1234
git merge recovered
🔝 Back to table of contents
7 - Complete solutions
Exercise 1 - Solution
git reset --soft HEAD~1
git commit -m "docs: add detailed usage section with examples"
Exercise 2 - Solution
git reset
# ou
git reset --mixed HEAD
Exercise 3 - Solution
git reset --hard HEAD~1
Exercise 4 - Solution
git reflog
# Trouver le hash du commit perdu
git reset --hard <hash>
Key takeaways
| Mode | Keeps the Working Dir | Keeps the Staging |
|---|---|---|
--soft | ✅ | ✅ |
--mixed | ✅ | ❌ |
--hard | ❌ | ❌ |
💡 Tip: When in doubt, use
--soft- it's the safest!