How to revert a commit in Git
Reverting a commit in Git safely undoes changes by creating a new commit that reverses the specified commit, preserving project history and maintaining safe collaboration practices. As the creator of CoreUI, a widely used open-source UI library, I’ve used git revert extensively across team development workflows to safely undo problematic commits without disrupting shared history. From my expertise, the most effective approach is using git revert to create inverse commits that undo specific changes while keeping history intact. This method provides safe change reversal that works well with shared repositories and team collaboration.
Use git revert to create a new commit that undoes the changes from a specific previous commit.
# Revert the last commit
git revert HEAD
# Revert a specific commit by hash
git revert abc1234
# Revert multiple commits
git revert HEAD~3..HEAD
# Revert without creating a commit (stage changes only)
git revert --no-commit HEAD
# Revert a merge commit (specify parent)
git revert -m 1 abc1234
# Revert and edit the commit message
git revert --edit HEAD
# Show what a revert would do without executing
git revert --no-commit HEAD
git status
# Then reset if you don't want to proceed
git reset --hard HEAD
The git revert command creates a new commit that undoes the changes from the specified commit. Unlike git reset, it doesn’t change history, making it safe for shared repositories. Use commit hashes or HEAD references to specify which commit to revert. For merge commits, use -m to specify which parent to revert to.
Best Practice Note:
This is the same git revert workflow we use in CoreUI development to safely undo problematic commits in shared repositories without affecting team collaboration.