0
0
Gitdevops~10 mins

Why knowing how to undo matters in Git - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why knowing how to undo matters
Make a change
Commit change
Realize mistake
Choose undo method
Undo last commit
Undo changes in working directory
Undo pushed commits
Fix mistake
Continue work
This flow shows how after making and committing changes, realizing a mistake leads to choosing the right undo method to fix it and continue working.
Execution Sample
Git
git commit -m "Add feature"
git reset --soft HEAD~1
git checkout HEAD -- file.txt
git reset file.txt
This sequence commits a change, then undoes the last commit but keeps changes staged, and finally discards changes in a file.
Process Table
StepCommandActionResult
1git commit -m "Add feature"Create a new commit with changesNew commit added to history
2git reset --soft HEAD~1Undo last commit but keep changes stagedCommit removed, changes ready to re-commit
3git checkout HEAD -- file.txt git reset file.txtDiscard changes in file.txtfile.txt restored to last committed state
4git statusCheck current stateNo changes to commit, working directory clean
💡 All mistakes undone, working directory and commit history restored as desired
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Commit HistoryInitial commitInitial commit + Add featureInitial commitInitial commitInitial commit
Staged ChangesNoneNoneAdd feature changes stagedNoneNone
Working DirectoryCleanCleanModified files from featureCleanClean
Key Moments - 2 Insights
Why does 'git reset --soft HEAD~1' keep changes staged instead of discarding them?
Because '--soft' moves the HEAD pointer back but leaves the index and working directory unchanged, so changes remain staged as shown in step 2 of the execution_table.
What happens if you use 'git checkout HEAD -- file.txt' and 'git reset file.txt' after resetting the commit?
It discards changes in file.txt by restoring it to the last committed state, as seen in step 3 where the working directory file is reverted.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the state of the commit history?
AA new commit is added
BThe last commit is removed but changes are still staged
CThe last commit is removed and changes are discarded
DNothing changes
💡 Hint
Refer to the 'Result' column in step 2 of the execution_table
At which step does the working directory become clean again?
AStep 1
BStep 3
CStep 4
DStep 2
💡 Hint
Check the 'Working Directory' variable in variable_tracker after each step
If you wanted to undo the last commit and discard all changes, which command would you use instead of 'git reset --soft HEAD~1'?
Agit reset --hard HEAD~1
Bgit checkout -- file.txt
Cgit commit --amend
Dgit revert HEAD
💡 Hint
Think about which reset option discards changes completely, not just moving HEAD
Concept Snapshot
Undoing mistakes in git is crucial to keep your project clean.
Use 'git reset --soft HEAD~1' to undo last commit but keep changes staged.
Use 'git checkout HEAD -- <file>' and 'git reset <file>' to discard changes in a file.
Choose the right undo method based on whether you want to keep or discard changes.
Knowing how to undo helps fix errors quickly and safely.
Full Transcript
This lesson shows why knowing how to undo in git matters. After making and committing changes, if you realize a mistake, you can undo the last commit with 'git reset --soft HEAD~1' which keeps your changes staged for fixing. To discard changes in a file, use 'git checkout HEAD -- file.txt' and 'git reset file.txt' which restores the file to the last committed state. The execution table traces these commands step-by-step, showing how commit history, staged changes, and working directory change. Key moments clarify why '--soft' keeps changes staged and how checkout and reset discard file changes. The quiz tests understanding of commit history state, when the working directory is clean, and which command discards changes fully. This knowledge helps you fix mistakes safely and continue working confidently.