Bird
Raised Fist0
Gitdevops~5 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Why is it important to know how to undo changes in Git?
easy
A. To create new branches
B. To fix mistakes and keep the project clean
C. To add more files to the repository
D. To merge two branches

Solution

  1. Step 1: Understand the purpose of undoing in Git

    Undoing helps correct errors made during development, preventing unwanted changes from affecting the project.
  2. Step 2: Recognize the benefit of a clean project history

    Keeping the project clean means the history is easier to read and maintain, which is important for teamwork and future updates.
  3. Final Answer:

    To fix mistakes and keep the project clean -> Option B
  4. Quick Check:

    Undoing fixes mistakes [OK]
Hint: Undoing fixes errors and keeps history clean [OK]
Common Mistakes:
  • Confusing undo with adding files
  • Thinking undo creates branches
  • Mixing undo with merging
2. Which Git command unstages files that were added to the staging area by mistake?
easy
A. git commit --amend
B. git checkout <file>
C. git reset HEAD <file>
D. git merge --abort

Solution

  1. Step 1: Identify the command to unstage files

    The command git reset HEAD <file> removes files from the staging area without deleting changes.
  2. Step 2: Understand why other commands don't unstage

    git commit --amend changes the last commit, git checkout <file> discards changes, and git merge --abort cancels a merge.
  3. Final Answer:

    git reset HEAD <file> -> Option C
  4. Quick Check:

    Unstage files = git reset HEAD [OK]
Hint: Use git reset HEAD to unstage files [OK]
Common Mistakes:
  • Using git commit --amend to unstage
  • Confusing checkout with unstaging
  • Trying to abort merge to unstage
3. What will be the output of the following commands if you want to discard changes in a file named app.js?
git status
git checkout -- app.js
git status
medium
A. The changes in app.js are discarded and it shows as unmodified
B. The changes are staged for commit
C. The file app.js is deleted from the project
D. The last commit is undone

Solution

  1. Step 1: Understand what git checkout -- app.js does

    This command discards local changes in app.js, restoring it to the last committed state.
  2. Step 2: Analyze the status before and after

    Before, git status shows changes in app.js. After, it shows no changes because they were discarded.
  3. Final Answer:

    The changes in app.js are discarded and it shows as unmodified -> Option A
  4. Quick Check:

    Checkout discards changes [OK]
Hint: git checkout -- filename discards local changes [OK]
Common Mistakes:
  • Thinking checkout stages changes
  • Believing checkout deletes files
  • Confusing checkout with undoing commits
4. You accidentally committed a file with sensitive data. Which command helps you undo the last commit but keep the changes in your working directory to fix the file?
medium
A. git checkout HEAD~1
B. git revert HEAD
C. git reset --hard HEAD~1
D. git reset --soft HEAD~1

Solution

  1. Step 1: Understand the difference between reset options

    git reset --soft HEAD~1 moves HEAD back one commit but keeps changes staged and in the working directory.
  2. Step 2: Why other options don't fit

    git revert HEAD creates a new commit undoing changes, git reset --hard HEAD~1 deletes changes, and git checkout HEAD~1 detaches HEAD without undoing commit properly.
  3. Final Answer:

    git reset --soft HEAD~1 -> Option D
  4. Quick Check:

    Undo last commit but keep changes = git reset --soft [OK]
Hint: Use git reset --soft to undo commit but keep changes [OK]
Common Mistakes:
  • Using --hard and losing changes
  • Using revert which adds a new commit
  • Using checkout which detaches HEAD
5. You want to undo multiple commits but keep the changes in your working directory to edit them before recommitting. Which sequence of commands achieves this safely?
hard
A. git reset --mixed HEAD~3; git add .; git commit -m "Updated commits"
B. git revert HEAD~3..HEAD; git push
C. git reset --hard HEAD~3; git commit -m "Undo commits"
D. git checkout HEAD~3; git commit -m "Undo commits"

Solution

  1. Step 1: Understand what git reset --mixed HEAD~3 does

    This command moves HEAD back 3 commits and unstages changes but keeps them in the working directory for editing.
  2. Step 2: Add and recommit after editing

    After editing, git add . stages changes and git commit -m "Updated commits" creates a new commit with the corrected changes.
  3. Step 3: Why other options are incorrect

    git revert creates new commits undoing changes, git reset --hard deletes changes, and git checkout detaches HEAD without undoing commits properly.
  4. Final Answer:

    git reset --mixed HEAD~3; git add .; git commit -m "Updated commits" -> Option A
  5. Quick Check:

    Undo commits but keep changes = git reset --mixed + add + commit [OK]
Hint: Use git reset --mixed to undo commits but keep changes [OK]
Common Mistakes:
  • Using git reset --hard and losing work
  • Using git revert which adds undo commits
  • Using git checkout which detaches HEAD