Mastering Git Revert: A Practical Insight from My DevOps Journey
As a DevOps engineer and CEO of Briller Technologies, I’m constantly navigating the intricate world of Git and GitHub. Today, I encountered a crucial concept — the ‘git revert’ command. It’s a lifesaver when you realize a specific commit wasn’t quite right.
The Situation: While working on a branch, I accidentally committed changes that, after some reflection, I realized weren’t ideal. But instead of stressing over it, I turned to ‘git revert’, a command that allows you to undo specific commits without rewriting history. This approach maintains the integrity of the commit history while introducing a new commit to reverse the changes.
How Git Revert Works When you revert a commit, Git creates a new commit that undoes the changes introduced by the specified commit. Unlike ‘git reset’, it does not alter the history. This makes it a safer and more collaborative-friendly approach.
Example Scenario: Imagine you have the following sequence of commits:
Commit ID 1 → Commit ID 2 → Commit ID 3 → Commit ID 4If you decide that the changes made in Commit ID 3 are not needed, you can revert it using the following steps.
Step-by-Step Guide to Revert a Commit
- View the Commit History:
git log --oneline
This command lists all the recent commits in a concise format, allowing you to easily identify the commit you want to revert.
2. Copy the Commit ID: Locate the commit you want to revert from the list. For instance, if the commit ID is abc123, copy it for the next step.
3. Revert the Commit:
git revert abc123
Git will create a new commit that undoes the changes made by commit abc123. During this process, Git may prompt you for a commit message. You can customize it or accept the default message provided.
Result: Once the revert process is complete, your commit history will look like this:
Commit ID 1 → Commit ID 2 → Commit ID 3 → Revert of Commit ID 3 → Commit ID 4Notice how the history remains intact, but a new commit appears to indicate that the changes from Commit ID 3 have been undone.
Key Takeaways:
- Unlike
git reset, thegit revertcommand maintains a clean, traceable commit history. - It’s the recommended approach for undoing changes in a collaborative environment.
- Each revert creates a new commit, so it’s easy to track the evolution of the project.
Here is the YouTube Link: https://youtube.com/@raeesq.?si=v_QK6Q2XXMf9mKep
I’m thrilled to share this learning experience with you. The ‘git revert’ command has reinforced my belief that mistakes are part of the process, and with the right tools, they’re easily fixable. If you found this blog useful, please share it with others so we can all continue growing as DevOps professionals.
Comments
Post a Comment