Bird
Raised Fist0
Gitdevops~20 mins

git revert to undo a commit safely - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Git Revert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of git revert HEAD?
You run git revert HEAD in a repository where the last commit added a file named notes.txt. What will happen?
AThe last commit is deleted from the history permanently.
BA new commit is created that removes <code>notes.txt</code> from the project.
CThe repository resets to the commit before the last one, discarding changes.
DGit shows an error saying revert cannot be done on the HEAD commit.
Attempts:
2 left
💡 Hint
Think about how git revert works compared to git reset.
🧠 Conceptual
intermediate
2:00remaining
Why is git revert safer than git reset for undoing commits in shared repositories?
Choose the best reason why git revert is preferred over git reset when working with others.
A<code>git revert</code> creates a new commit that undoes changes, preserving history and avoiding conflicts.
B<code>git revert</code> deletes the commit from history, making the repository cleaner.
C<code>git revert</code> automatically merges branches after undoing commits.
D<code>git revert</code> rewrites all commit messages to keep them consistent.
Attempts:
2 left
💡 Hint
Think about what happens to commit history and collaboration.
🔀 Workflow
advanced
2:00remaining
Which command sequence correctly reverts a specific commit safely?
You want to undo the commit with hash abc1234 without affecting other commits. Which command sequence is correct?
Agit reset --hard abc1234
Bgit checkout abc1234 && git commit --amend
Cgit revert abc1234
Dgit revert HEAD~3
Attempts:
2 left
💡 Hint
Focus on reverting a specific commit by its hash.
Troubleshoot
advanced
2:00remaining
What error occurs if you try to revert a merge commit without special options?
You run git revert on a merge commit without any extra flags. What error message will Git show?
Awarning: merge conflicts detected but revert completed.
Bfatal: You are not currently on a branch.
Cerror: cannot revert because working directory is dirty.
Derror: commit <hash> is a merge but no -m option was given.
Attempts:
2 left
💡 Hint
Think about what Git needs to know when reverting merges.
Best Practice
expert
3:00remaining
What is the recommended way to undo a commit that has already been pushed to a shared repository?
You accidentally pushed a commit with sensitive data. What is the safest way to undo it without disrupting others?
AUse <code>git revert</code> to create a new commit that removes the sensitive data.
BUse <code>git reset --hard</code> to remove the commit and force push to overwrite history.
CUse <code>git rebase -i</code> to remove the commit and then push normally.
DDelete the remote branch and push a new one without the commit.
Attempts:
2 left
💡 Hint
Consider the impact on collaborators and history rewriting.

Practice

(1/5)
1. What does the git revert command do in a Git repository?
easy
A. It merges two branches together.
B. It deletes the commit from the project history permanently.
C. It resets the branch to a previous commit without creating a new commit.
D. It creates a new commit that undoes the changes of a previous commit.

Solution

  1. Step 1: Understand the purpose of git revert

    git revert creates a new commit that reverses the changes made by a specified previous commit.
  2. Step 2: Compare with other commands

    Unlike git reset, it does not remove commits from history but safely adds a new commit to undo changes.
  3. Final Answer:

    It creates a new commit that undoes the changes of a previous commit -> Option D
  4. Quick Check:

    git revert = new commit undoing changes [OK]
Hint: Remember: revert adds a new commit to undo changes [OK]
Common Mistakes:
  • Confusing revert with reset which removes commits
  • Thinking revert deletes commits permanently
  • Assuming revert merges branches
2. Which of the following is the correct syntax to revert the latest commit in Git?
easy
A. git revert HEAD
B. git revert --latest
C. git revert -m HEAD
D. git revert --undo HEAD

Solution

  1. Step 1: Identify the correct command to revert the latest commit

    The latest commit is referenced by HEAD, and the correct command is git revert HEAD.
  2. Step 2: Check invalid options

    Options like --latest, -m without context, or --undo are not valid revert flags.
  3. Final Answer:

    git revert HEAD -> Option A
  4. Quick Check:

    Revert latest commit = git revert HEAD [OK]
Hint: Use HEAD to revert the latest commit safely [OK]
Common Mistakes:
  • Using invalid flags like --latest or --undo
  • Confusing revert options with reset options
  • Trying to revert without specifying a commit
3. Given the following Git commands executed in order:
git commit -m "Add feature A"
git commit -m "Fix bug B"
git revert HEAD

What will be the result of the last command?
medium
A. The commit "Fix bug B" is deleted from history.
B. The branch is reset to the commit "Add feature A" without a new commit.
C. A new commit is created that undoes the changes from "Fix bug B".
D. An error occurs because HEAD cannot be reverted.

Solution

  1. Step 1: Identify the commit pointed by HEAD

    After two commits, HEAD points to "Fix bug B" commit.
  2. Step 2: Understand what git revert HEAD does

    It creates a new commit that reverses the changes introduced by the latest commit "Fix bug B".
  3. Final Answer:

    A new commit is created that undoes the changes from "Fix bug B" -> Option C
  4. Quick Check:

    git revert HEAD = new commit undoing latest commit [OK]
Hint: Revert HEAD always creates a new undo commit [OK]
Common Mistakes:
  • Thinking revert deletes commits from history
  • Confusing revert with reset which moves HEAD
  • Assuming revert causes errors on HEAD
4. You ran git revert abc123 but got a merge conflict error. What should you do to fix this?
medium
A. Run git revert --abort to cancel the revert and try again.
B. Manually resolve the conflicts, then run git revert --continue.
C. Delete the commit abc123 and try reverting again.
D. Use git reset --hard to fix the conflict.

Solution

  1. Step 1: Understand merge conflicts during revert

    Revert can cause conflicts if changes overlap. You must resolve conflicts manually.
  2. Step 2: Continue revert after resolving conflicts

    After fixing conflicts, run git revert --continue to complete the revert commit.
  3. Final Answer:

    Manually resolve the conflicts, then run git revert --continue -> Option B
  4. Quick Check:

    Resolve conflicts + git revert --continue = fix revert conflict [OK]
Hint: Fix conflicts, then run git revert --continue [OK]
Common Mistakes:
  • Trying to delete commits to fix revert conflicts
  • Using git reset which discards changes unsafely
  • Aborting revert without resolving conflicts
5. You want to undo a commit that was pushed and shared with your team, but keep the project history intact. Which approach is safest?
hard
A. Use git revert <commit-hash> to create a new commit that undoes the changes.
B. Use git reset --hard <commit-hash> and force push to rewrite history.
C. Delete the commit from the remote repository manually.
D. Use git checkout <commit-hash> to switch to the previous commit.

Solution

  1. Step 1: Consider shared repository safety

    When commits are shared, rewriting history (reset + force push) can cause problems for others.
  2. Step 2: Use revert to safely undo changes

    git revert adds a new commit that reverses changes without rewriting history, keeping the project safe and intact.
  3. Final Answer:

    Use git revert <commit-hash> to create a new commit that undoes the changes -> Option A
  4. Quick Check:

    Revert = safe undo for shared commits [OK]
Hint: Revert to undo shared commits safely, never reset [OK]
Common Mistakes:
  • Using reset and force push on shared branches
  • Deleting commits manually from remote
  • Checking out old commits without reverting