How to use the command git revert (with examples)

How to use the command git revert (with examples)

Git is a distributed version control system that allows developers to manage changes to their codebase and collaborate with others. The git revert command is used to create new commits that reverse the effect of earlier commits. This is useful when you need to undo changes that have been committed to the repository.

Use case 1: Revert the most recent commit

Code:

git revert HEAD

Motivation: Sometimes we make a mistake in the most recent commit and need to undo it. Reverting the most recent commit allows us to easily undo the changes without modifying the commit history.

Explanation:

  • HEAD is a reference to the most recent commit on the current branch. Specifying HEAD as the argument tells Git to revert the changes made in the most recent commit.
  • By default, git revert creates a new commit with the changes that reverse the effect of the specified commit.

Example output:

Revert "Update file.txt"

This reverts commit 0c01a9f506247d0dd3ab7b5daa369d5a0e78c456.

Use case 2: Revert the 5th last commit

Code:

git revert HEAD~4

Motivation: Reverting a specific commit that is not the most recent one can be useful when you want to undo changes made in a certain commit, but not the subsequent ones. This allows you to selectively revert commits in the commit history.

Explanation:

  • HEAD~4 is a reference to the commit that is fifth before the most recent commit. Specifying HEAD~4 tells Git to revert the changes made in that commit.
  • By default, git revert creates a new commit with the changes that reverse the effect of the specified commit.

Example output:

Revert "Fix bug in file.py"

This reverts commit 69f4b52d96304e9b7fdbf82def51d62c574d42f8.

Use case 3: Revert a specific commit

Code:

git revert 0c01a9

Motivation: There may be cases where you want to revert a specific commit that is not the most recent one. This allows you to undo the changes made in that commit while keeping the subsequent commits intact.

Explanation:

  • 0c01a9 is the hash of the specific commit that you want to revert. Specifying the commit hash tells Git to revert the changes made in that commit.
  • By default, git revert creates a new commit with the changes that reverse the effect of the specified commit.

Example output:

Revert "Add feature X"

This reverts commit 0c01a9f506247d0dd3ab7b5daa369d5a0e78c456.

Use case 4: Revert multiple commits

Code:

git revert branch_name~5..branch_name~2

Motivation: When you want to undo changes made in multiple consecutive commits, you can use the range of commit references. This allows you to easily revert a series of commits with a single command.

Explanation:

  • branch_name~5..branch_name~2 is a commit range that specifies the commits you want to revert, from the 5th last commit to the 2nd last commit. Specifying the commit range tells Git to revert the changes made in those commits.
  • By default, git revert creates a new commit for each commit in the specified range with the changes that reverse the effect of each commit.

Example output:

Revert "Implement feature Y"

This reverts commit 71462ff4b772ae15d529c3fd7fdc139c8b8dbf92.

Revert "Fix bug in feature X"

This reverts commit 69f4b52d96304e9b7fdbf82def51d62c574d42f8.

Use case 5: Don’t create new commits, just change the working tree

Code:

git revert -n 0c01a9..9a1743

Motivation: There may be scenarios where you want to revert changes made in multiple commits but do not want to create new commits for each revert. This can be useful when you want to have more control over the process and review the changes before committing.

Explanation:

  • -n is an option that tells Git to perform the revert operation, but not create new commits. Instead, it stages the changes in the working tree, allowing you to review and modify them further.
  • 0c01a9..9a1743 is a commit range that specifies the range of commits you want to revert. Specifying the commit range tells Git to revert the changes made in those commits.

Example output:

Reverting "Add feature A"

Reverting "Implement feature B"

Reverting "Fix bug in feature C"

...

Conclusion:

The git revert command is a powerful tool for undoing changes in Git. It allows you to easily revert commits, whether they are the most recent ones or specific ones in the commit history. By using the appropriate arguments and options, you can selectively revert changes and control the outcome of the operation.

Related Posts

w32tm Command Examples (with examples)

w32tm Command Examples (with examples)

Example 1: Show the current status of time synchronization w32tm /query /status /verbose Motivation The motivation for checking the current status of time synchronization is to ensure that the clock on the system is synchronized correctly with a reliable time source.

Read More
How to use the command 'sysctl' (with examples)

How to use the command 'sysctl' (with examples)

Sysctl is a command-line tool that allows users to view and modify kernel runtime variables on a Unix-like operating system.

Read More
Using mkfs.btrfs (with examples)

Using mkfs.btrfs (with examples)

1: Create a btrfs filesystem on a single device sudo mkfs.

Read More