0
0
Gitdevops~5 mins

Why knowing how to undo matters in Git - Why It Works

Choose your learning style9 modes available
Introduction
Mistakes happen when working with code. Knowing how to undo changes helps you fix errors quickly without losing work or causing bigger problems.
When you accidentally add wrong files to your commit and want to remove them before pushing.
When you realize your last commit message has a typo and want to correct it.
When you want to discard local changes that you no longer need.
When you want to go back to a previous version of your project safely.
When you want to undo a merge that caused conflicts or issues.
Commands
Check the current state of your working directory and staging area to see what changes exist.
Terminal
git status
Expected OutputExpected
On branch main Your branch is up to date with 'origin/main'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: example.txt no changes added to commit (use "git add" and/or "git commit -a")
Discard changes in the file 'example.txt' and revert it to the last committed state.
Terminal
git restore example.txt
Expected OutputExpected
No output (command runs silently)
--staged - Undo changes that have been staged for commit
Unstage the file 'example.txt' if it was added to the staging area by mistake.
Terminal
git reset HEAD example.txt
Expected OutputExpected
Unstaged changes after reset: M example.txt
Change the last commit message to fix a typo or improve the message before pushing.
Terminal
git commit --amend -m "Fix typo in commit message"
Expected OutputExpected
[main 1a2b3c4] Fix typo in commit message Date: Thu Jun 15 10:00:00 2024 +0000 1 file changed, 2 insertions(+), 1 deletion(-)
--amend - Modify the last commit
-m - Provide a new commit message inline
View the last three commits in a short format to confirm your changes.
Terminal
git log --oneline -3
Expected OutputExpected
1a2b3c4 Fix typo in commit message 9f8e7d6 Add new feature 3c4d5e6 Initial commit
Key Concept

If you remember nothing else from this pattern, remember: git lets you safely undo mistakes to keep your work clean and correct.

Common Mistakes
Using 'git reset' without understanding it can delete changes permanently.
Resetting can remove commits or changes if used incorrectly, causing data loss.
Use 'git restore' to discard changes safely and 'git reset' carefully with knowledge of its effects.
Trying to change a commit message after pushing to a shared repository.
Changing pushed commits rewrites history and can confuse collaborators.
Only amend commits before pushing; if already pushed, use a new commit to fix mistakes.
Summary
Use 'git restore' to discard unwanted local changes safely.
Use 'git reset' to unstage files accidentally added to the commit.
Use 'git commit --amend' to fix the last commit message before pushing.
Check your changes and commits with 'git status' and 'git log' to stay informed.