To revert a commit in Git, use the command:
git revert <commit-ID>
This command creates a new commit that undoes the changes made by the
specified commit without altering the commit history. First, get the unique
commit ID you want to revert by using git log
. Then run the revert command
with this ID. This adds a new commit that reverses the changes of the targeted
commit. Key points:
git revert
does not remove the original commit; instead, it adds a new commit that negates it.- This method is safe for published commits because it maintains the commit history.
- For multiple commits, use a range like
git revert <older-commit-ID>..<newer-commit-ID>
to revert them all. - Tools like GitKraken or GitLens in VS Code provide graphical ways to revert commits with a few clicks.
In summary, git revert
is the safest and most common way to undo a commit
while preserving the commit history in Git repositories.