0
0
Gitdevops~30 mins

Difference between reset and revert in Git - Hands-On Comparison

Choose your learning style9 modes available
Difference between reset and revert in Git
📖 Scenario: You are working on a project using Git for version control. Sometimes you make changes that you want to undo. Git offers two main ways to undo changes: reset and revert. Understanding the difference helps you choose the right command to fix mistakes safely.
🎯 Goal: Learn how to use git reset and git revert commands to undo changes in different ways. You will create a simple Git repository, make commits, then undo changes using both commands to see how they behave differently.
📋 What You'll Learn
Create a Git repository and make commits
Use git reset to undo commits locally
Use git revert to undo commits safely by creating new commits
Observe the commit history after each command
💡 Why This Matters
🌍 Real World
Developers often need to undo mistakes in code history. Knowing when to use reset or revert helps keep the project history clean and safe.
💼 Career
Understanding these commands is essential for software developers, DevOps engineers, and anyone working with Git in team environments.
Progress0 / 4 steps
1
Initialize Git repository and create commits
Run the following commands to initialize a Git repository, create a file called file.txt, add some text, and make two commits with messages First commit and Second commit:
git init
echo "Line 1" > file.txt
git add file.txt
git commit -m "First commit"
echo "Line 2" >> file.txt
git add file.txt
git commit -m "Second commit"
Git
Need a hint?

Use git init to start a repo, then create and commit changes step by step.

2
Use git reset to undo the last commit locally
Use the command git reset --soft HEAD~1 to undo the last commit but keep the changes staged. This moves the HEAD pointer back by one commit without deleting your changes.
Git
Need a hint?

git reset --soft HEAD~1 moves HEAD back one commit but keeps changes staged.

3
Use git revert to undo the last commit safely
Use the command git revert HEAD@{1} to create a new commit that undoes the changes made in the commit you just reset (the previous HEAD position, referenced as HEAD@{1}). This keeps the history intact and is safe for shared repositories.
Git
Need a hint?

git revert HEAD@{1} creates a new commit that reverses the changes of the previously reset commit.

4
Show the commit history to compare reset and revert effects
Run git log --oneline to display the commit history. Observe how git reset removed the last commit from history, while git revert added a new commit that undoes the last commit.
Git
Need a hint?

Use git log --oneline to see the short commit history.