How To Revert A Commit in Git (Step-by-Step Guide)
Introduction to Git and Version Control
Git is a distributed version control system designed to help developers track changes in their codebases. It is widely used in software development to collaborate efficiently on projects. Git is open-source and supported by platforms like GitHub, GitLab, and Bitbucket.
One of the essential features of Git is the ability to commit changes, allowing users to take snapshots of their work. However, there are times when developers need to undo a commit due to errors or unwanted changes. This is where Git revert comes in.
What is a Git Commit?
A commit in Git represents a snapshot of the project’s state at a given time. Every commit contains:
- A unique commit hash
- A commit message describing the changes
- References to parent commits (if applicable)
Syntax of Git Commit Command
git commit -m "commit message"
To learn more about commits, check out this guide on Git commits.
What is Git Revert?
Git revert is a command used to undo a specific commit while keeping the project history intact. Instead of deleting the commit, Git revert creates a new commit that reverses the changes from the specified commit.
Syntax of Git Revert Command
git revert <commit-hash>
Key Points to Remember
- The original commit remains in the history.
- A new commit is created to undo the changes.
- It is preferred for shared branches as it does not rewrite history.
Git Revert vs Git Reset
| Feature | Git Revert | Git Reset |
|---|---|---|
| Effect | Creates a new commit to undo changes | Moves HEAD to a previous commit |
| Commit History | Preserved | Rewritten |
| Best for | Shared branches | Local changes |
| Syntax | git revert <commit-hash> |
git reset <commit-hash> |
To understand Git reset in detail, read this guide on Git reset.
Steps to Revert a Commit
Step 1: Find the Commit Hash
Use the git log command to list all commits along with their commit messages and hashes.
git log
The output will show:
commit abc123def456
Author: John Doe <[email protected]>
Date: Wed Mar 6 12:00:00 2024 +0000
Fixed a bug in authentication
Step 2: Revert the Commit
Once the commit hash is identified, use the following command:
git revert abc123def456
Step 3: Resolve Conflicts (If Any)
If there are merge conflicts, resolve them manually or use:
git mergetool
Step 4: Edit the Commit Message (Optional)
Git will open a text editor (Vim by default) to edit the commit message. Modify it if necessary and save.
Step 5: Push the Changes
After the commit is reverted, push the changes to the remote repository:
git push origin <branch-name>
Advanced Git Revert Options
1. Revert Without Committing
If you want to revert changes without creating a commit, use:
git revert --no-commit <commit-hash>
This will stage the reverted changes, allowing you to modify them before committing.
2. Reverting a Merge Commit
If you need to revert a merge commit, specify which parent’s changes should be reversed:
git revert -m 1 <merge-commit-hash>
Here, -m 1 indicates which parent commit should be retained.
3. Revert Without Opening an Editor
If you want to avoid editing the commit message, use:
git revert --no-edit <commit-hash>
How To Revert A Commit in Git Example Scenario
Initial Setup
Let’s create a new Git repository and make some commits.
git init my-project
cd my-project
echo "Hello, World!" > intro.txt
git add .
git commit -m "Initial commit"
Making a Change
echo "Adding new content" > intro.txt
git add .
git commit -m "Updated intro.txt"
Finding the Commit Hash
git log --oneline
Output:
abc123 (HEAD -> main) Updated intro.txt
def456 Initial commit
Reverting the Last Commit
git revert abc123
Checking the Log After Reverting
git log --oneline
Output:
ghi789 (HEAD -> main) Revert "Updated intro.txt"
def456 Initial commit
The new commit ghi789 has undone the changes from commit abc123.
Pushing the Changes
git push origin main
Best Practices for Using Git Revert
- Use revert instead of reset when working on shared branches.
- Check the commit history before reverting.
- Resolve merge conflicts carefully when reverting commits.
- Test after reverting to ensure the project is stable.
Conclusion
Git revert is a powerful tool for undoing changes while maintaining a clean commit history. By following the steps outlined above, developers can safely revert commits without disrupting collaboration. Whether you’re fixing a mistake or rolling back a faulty update, Git revert ensures your repository remains stable and organized.
For more insights on Git commands, check out the official Git documentation.
Explore more about how to run git commands in windows
