Reverting a Commit
If you want to reverse the effects of a commit without rewriting the project history, thegit revert command is the best option. For example, suppose you committed the addition of a third story (i.e., adding third_story.md), but later determined that change should be undone. Running the command below will generate a new commit that cancels out the changes introduced by the specified commit:

third_story.md—while preserving your project’s commit history.
Using
git revert is particularly helpful in collaborative environments because it maintains the integrity of shared history.Resetting a Commit
Another method to undo changes is by using thegit reset command. The reset command comes in multiple forms, allowing you to either preserve or discard the changes made in a commit.
Soft Reset
A soft reset moves the branch pointer back to a previous commit without altering the working directory or staging area. This approach lets you review and re-commit the changes if needed. For example, to reset the last commit (which addedthird_story.md) while keeping its changes staged, execute:
Hard Reset
In contrast, a hard reset moves the branch pointer and discards all changes in the working directory related to the commit. Using a hard reset for thethird_story.md commit will completely remove the file:
A hard reset is irreversible for local changes. Ensure you truly want to discard all modifications before using this command.
Summary
| Command | Description | Example |
|---|---|---|
| git revert <commit> | Creates a new commit that reverses changes while preserving commit history. | $ git revert 8ad5d |
| git reset —soft HEAD~1 | Moves the branch pointer back one commit, keeping changes in the staging area for review. | $ git reset --soft HEAD~1 |
| git reset —hard HEAD~1 | Moves the branch pointer back one commit and discards all changes in the working directory. | $ git reset --hard HEAD~1 |