
Undoing Changes: Reverting Commits and Resetting Branches
Even with the best planning, sometimes you'll want to undo changes you've made. Git provides powerful tools to rewind your project's history. It's crucial to understand that undoing changes, especially in a shared repository, has implications. We'll cover two primary ways to do this: reverting commits and resetting branches.
When you revert a commit, Git doesn't actually delete the old commit. Instead, it creates a new commit that undoes the changes introduced by a previous commit. This is a safe way to undo changes because it preserves the history of what happened, making it easier to track why a change was undone. It's generally the preferred method for undoing changes that have already been pushed to a shared remote repository.
To revert a commit, you'll use the git revert command followed by the commit hash of the commit you want to undo.
git revert <commit_hash>Let's say you want to revert the commit with the hash a1b2c3d4. You would run:
git revert a1b2c3d4Git will then open your default text editor to allow you to write a commit message for this new revert commit. The default message will explain which commit is being reverted. After saving and closing the editor, the revert commit will be created.