Git Reset - Practical Exercise

1 min

Table of contents

  1. Exercise Setup
  2. Exercise 1: Reset Soft
  3. Exercise 2: Reset Mixed
  4. Exercise 3: Reset Hard
  5. Verification

1. Exercise Setup

bash
mkdir exercice-reset && cd exercice-reset
git init

# Create several commits to test
echo "print('v1')" > app.py && git add . && git commit -m "Version 1"
echo "print('v2')" > app.py && git add . && git commit -m "Version 2"
echo "print('v3')" > app.py && git add . && git commit -m "Version 3"
echo "print('v4')" > app.py && git add . && git commit -m "Version 4"

git log --oneline    # Note the hashes

Back to table of contents

2. Exercise 1: Reset Soft

Goal: Go back 2 commits, keep the changes staged.

bash
git reset --soft HEAD~2

# Checks
git status           # Changes in the staging area?
cat app.py          # What content?
git log --oneline   # How many commits?

Expected result:

  • File: contains v4
  • Status: modifications staged for commit
  • Log: 2 commits (v1, v2)

Back to table of contents

3. Exercise 2: Reset Mixed

Reset then mixed test:

bash
git commit -m "Re-commit v4"    # Put v4 back
git reset --mixed HEAD~1        # Go back 1 commit (default mode)

# Checks
git status           # Unstaged changes?
cat app.py          # What content?
git log --oneline   # How many commits?

Expected result:

  • File: contains v4
  • Status: unstaged modifications
  • Log: 3 commits (v1, v2, v3)

Back to table of contents

4. Exercise 3: Reset Hard

WARNING: Hard reset = destructive!

bash
git add .                    # Stage the changes
git commit -m "Re-commit v4" # Put v4 back
git reset --hard HEAD~2      # Delete EVERYTHING, go back 2 commits

# Checks
git status           # Clean working directory?
cat app.py          # What content?
git log --oneline   # How many commits?

Expected result:

  • File: contains v2
  • Status: nothing to commit
  • Log: 2 commits (v1, v2)

Back to table of contents

5. Verification

Recap of the 3 modes:

ModeCommitsStagingWorking Directory
--softMoves backKeepsKeeps
--mixedMoves backDeletesKeeps
--hardMoves backDeletesDeletes

You understood the 3 modes? Perfect! Reset has no more secrets.

Back to table of contents