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 hashesGoal: Go back 2 commits, keep the changes staged.
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:
v4Reset then mixed test:
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:
v4WARNING: Hard reset = destructive!
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:
v2Recap of the 3 modes:
| Mode | Commits | Staging | Working Directory |
|---|---|---|---|
--soft | Moves back | Keeps | Keeps |
--mixed | Moves back | Deletes | Keeps |
--hard | Moves back | Deletes | Deletes |
You understood the 3 modes? Perfect! Reset has no more secrets.