Git Revert Commit Command Tutorial

Git provides the git revert command in order to revert an existing commit. The git revert can be used with a commit checksum to revert back to this commit easily. The git revert command can be also used with different commit specifiers like HEAD , Commit Checksum , Branch Name etc

git revert Command Syntax

The git revert command has the following simple syntax where the HEAD, Commit Checksum, or Branch name is used as commit specifier.

git OPTION COMMIT
  • OPTION is optional and used for different options.
  • COMMIT is used to specify the commit we want to revert back. The COMMIT can be the HEAD, Commit Checksum or Branchname.

git revert with Commit Checksum

One of the most popular usages for the git revert is providing the Commit checksum to specify the commit we want to revert. The Commit Checksum is provided as a parameter to the git revert command.

$ git revert c3d9d16744dc445f

git revert with HEAD Commit

The HEAD can be used to specify the commit we want to revert back. The tilda ~ sign is used to specify the count of commits before the HEAD. In the following example, we revert 3 commits back from the HEAD.

$ git revert HEAD~3

git revert with Branch Name

The branch names also provide easy access to the specific commit in a specific branch. The git revert can be used with branch names. The tilda ~ sign is used to specify the count of commits before the head of the specified branch. In the following example, we revert 5 commits back from the head of the “test” branch.

$ git revert test~5

git revert Without Editing Messages

By default, the git revert command opens the editor to edit Git commit messages. The reverted intermediate messages are edited by default. But we can skip the editing messages by using the --no-edit option.

$ git revert --no-edit c3d9d16744dc445f

Leave a Comment