0
0
Gitdevops~15 mins

git revert to undo a commit safely - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

The output should show three commits including the revert commit.