Skip to main content

Exercise: Using Git Reset


Table of contents


  1. Objectives
  2. Setup
  3. Exercise 1: Reset --soft
  4. Exercise 2: Reset --mixed
  5. Exercise 3: Reset --hard
  6. Exercise 4: Recovery with reflog
  7. Solutions


1 - Objectives



By the end of this exercise, you will know how to:

  • ✅ Use git reset --soft to modify a commit
  • ✅ Use git reset --mixed to reorganize the staging
  • ✅ Use git reset --hard to 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

  1. Use git reset --soft to undo the last commit
  2. Verify that the changes are still in staging
  3. 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

  1. After the reset --soft, are the files in staging or in the working directory?
  2. 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:

  1. The files are in staging (Changes to be committed)
  2. 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

  1. Create two new files and add them to staging
  2. Use git reset to remove them from staging
  3. 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

  1. What is the difference between git reset and git reset --mixed?
  2. 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:

  1. No difference! --mixed is the default option
  2. 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

  1. Make "broken" changes
  2. Use git reset --hard to discard everything
  3. 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

  1. After the reset --hard, can you recover the broken.txt file?
  2. 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:

  1. Yes, with git reflog and git checkout <hash> -- broken.txt
  2. Do a git stash beforehand, 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

  1. Make an "important" commit
  2. Delete it with reset --hard
  3. 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

ModeKeeps the Working DirKeeps the Staging
--soft
--mixed
--hard

💡 Tip: When in doubt, use --soft - it's the safest!

🔝 Back to table of contents