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
Undo a Commit Safely Using git revert
📖 Scenario: You are working on a project and accidentally committed a change that you want to undo without losing the history of your work.
🎯 Goal: Learn how to safely undo a commit using git revert so that the commit history remains intact and the unwanted changes are undone.
📋 What You'll Learn
Create a git repository with an initial commit
Make a second commit with a change
Use git revert to undo the second commit
Show the commit log to confirm the revert
💡 Why This Matters
🌍 Real World
In real projects, mistakes happen. Using <code>git revert</code> helps undo changes without losing history, which is important for teamwork and tracking.
💼 Career
Knowing how to safely undo commits is a key skill for developers and DevOps engineers to maintain clean and reliable project histories.
Progress0 / 4 steps
1
Initialize a Git repository and create the first commit
Run the commands to initialize a git repository, create a file named file.txt with the content Hello World, add it to git, and commit with the message Initial commit.
Git
Hint
Use git init to start the repository. Use echo to create the file. Then add and commit.
2
Make a second commit with a change
Modify file.txt by adding the line This is a second line, then add and commit the change with the message Added second line.
Git
Hint
Use >> to append text to the file. Then add and commit the change.
3
Undo the second commit safely using git revert
Use git revert with the commit hash of the second commit to create a new commit that undoes the changes from the second commit.
Git
Hint
Use git revert HEAD to undo the last commit safely.
4
Show the commit log to confirm the revert
Run git log --oneline to display the commit history and confirm that the revert commit is present.
Git
Hint
The output should show three commits including the revert commit.
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
Step 1: Understand the purpose of git revert
git revert creates a new commit that reverses the changes made by a specified previous commit.
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.
Final Answer:
It creates a new commit that undoes the changes of a previous commit -> Option D
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
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.
Step 2: Check invalid options
Options like --latest, -m without context, or --undo are not valid revert flags.
Final Answer:
git revert HEAD -> Option A
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: