Reverting Commits
When you need to undo a commit that has already been pushed and shared,
git revert is the safe choice. Instead of erasing history, it creates a new commit that undoes the changes of a previous commit.What Is Revert?
git revert takes a commit, calculates the inverse of its changes, and creates a new commit that applies that inverse. The original commit stays in the history, but its effect is canceled.Reverting the Last Commit
To undo the most recent commit:
git revert HEADThis opens your editor for a commit message (you can use -m to supply one inline).Reverting an Earlier Commit
Find the commit hash using
git log, then:git revert a1b2c3dThis creates a new commit that undoes the changes introduced by that specific commit.Revert vs. Reset
- Reset: Moves the branch pointer, effectively erasing commits. Only safe for local, unpublished work.
- Revert: Adds a new commit that reverses a previous commit. Safe for shared branches because it doesn’t rewrite history.
Handling Merge Conflicts in Revert
If reverting causes conflicts, resolve them as you would in a merge, then
git add and git commit.Two Minute Drill
git revertcreates a new commit that undoes a previous commit.- It is safe for shared branches because it doesn’t rewrite history.
- Use
git revert HEADto undo the last commit. - Reset erases commits; revert adds a new commit.
Need more clarification?
Drop us an email at career@quipoinfotech.com
