The git revert Command

Learn to use the git revert command.

The git revert command

If we change something and then regret it later, we can use git revert to revert the changes.

For example, if we change a function name in a commit and afterward want to revert the change, we can use git revert HASH_ID to pick the changes in that particular commit and undo them. That will then change the function name back to the original name.

Instead of removing the commit from history, git revert finds out how to invert the changes applied by that commit and appends a new commit with the inverse content.

Significance

The git revert command prevents Git from losing the history, which is very important for the integrity of revision history and reliable collaboration.

We can use git revert to apply the inverse of any commit from the project history. Suppose we track a bug in our code and find out that a specific commit introduced it. We can manually fix it and commit a new snapshot. Or, we can use git revert to do all of this automatically.

Get hands-on with 1200+ tech skills courses.