How to revert a commit in Git
Reverting commits safely is essential for undoing problematic changes in shared repositories without disrupting other developers’ work or rewriting project history.
As the creator of CoreUI, a widely used open-source UI library, I’ve safely reverted countless commits in production repositories over 25 years of development.
From my expertise, the safest approach is using git revert, which creates a new commit that undoes the changes from a previous commit.
This maintains the integrity of the project history while effectively canceling out unwanted changes.
Use git revert to create a new commit that undoes changes from a previous commit.
git revert abc123f
git revert HEAD~2
Here the first command reverts the specific commit with hash abc123f, creating a new commit that undoes those changes. The second command reverts the commit that was 2 commits before the current HEAD. Git automatically creates a commit message describing the revert, but you can edit it if needed. This approach is safe for shared repositories because it doesn’t rewrite history.
Best Practice Note:
This is the same safe revert strategy we use in CoreUI project maintenance for production code corrections.
Use git revert instead of git reset for commits that have been pushed to shared repositories, as it preserves history and prevents conflicts for other developers.



