Git revert Command


In Git, the git revert command is used to undo a previous commit by creating a new commit that reverses the changes made by the previous commit. This is different from the git reset command, which simply discards the changes made by a commit without creating a new commit. Here are some examples of how to use git revert:

Reverting a single commit:

git revert <commit-hash>

This command will create a new commit that undoes the changes made by the specified commit.

Reverting a range of commits:

git revert <start-commit>..<end-commit>

This command will create a new commit that undoes the changes made by all commits between the specified start and end commits.

Reverting a merge commit:

git revert -m <parent-number> <commit-hash>

This command will revert the changes made by a merge commit. The -m option is used to specify which parent of the merge commit to revert.

Here’s an example of how to use git revert to undo a previous commit:

Suppose you have made a commit that introduced a bug and you want to undo that commit. Here are the steps you would follow:

  1. Find the hash of the commit you want to revert by using the command git log.
  2. Revert the commit using the command git revert <commit-hash>.
  3. Git will open a text editor where you can enter a commit message for the revert. Save and close the file.
  4. Git will create a new commit that undoes the changes made by the previous commit.
  5. Use git log again to confirm that the revert commit has been created.

Note that the git revert command does not delete the original commit or any subsequent commits that were made after the original commit. It simply creates a new commit that undoes the changes made by the original commit. This means that you can safely use git revert to undo a commit even if other people have already pulled the original commit into their repositories.