Open In App

How to Revert a Git Commit

Last Updated : 03 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

While working on a project, there is a high chance of making some mistakes, but finding that bug and making the code correction are equally important to make a good project. And, if you have committed the wrong changes, Git allows you to revert the Git Commit to undo the effect of a particular commit. This property makes it a safe and preferred method to handle rollbacks when collaborating in a team.

In this article, we will take you through a step-by-step journey on how to revert a Git commit. You will learn how to identify the commit that you want to revert, create the revert commit, and make sure the changes are accurately undone while maintaining your repository’s history.

Reverting a Git Commit

Git provides an option to keep track of our project progress from revisiting older commits to mending our mistakes and undoing changes. It gives the user to option to review his/her work. The following commands perform similar functions.

Step-1: git log

Before reverting a Git Commit, first, check all the commits and find hashing of all the commits that we have done and select the targeted Commit that we want to revert using the below command

git log

Step-2: git revert

As the name suggests, this command is used to return some existing commits. This command introduces a new commit to reverse the effect of some previous faulty commit. This option does not move branch ref pointers to the faulty commit instead inverse the changes from that commit and create a new revert commit. In such a way, project history is maintained. But to execute this command your working tree should be clean.

git revert <commit_hash>

change your commit hash with the ‘<commit_hash>”

git  revert

git revert options:

  • -e or –edit: This option lets you edit the commit message prior to committing the revert. It is the default option.
  • –no-edit: Using this option will not start committing the text editor.
  • -n or –no-commit: Using this option add inverse changes to the staging index and working tree instead of making new commits.

git reset:

It resets the current head to the specified state. This git command is not commonly used to its restrictive use. It has three basic Options namely, –soft, –hard, and –mixed.

git rm:

This command is used to remove the tracked file from the index or from the working tree and git index. It cannot remove files just from the working directory. The files removed must be identical to those in the current head.

git rm options:

  • (files) …: It specifies the files to be removed. There can be an individual file or a list of files separated by a space character.
  • -f or –force: It overrides up-to-date check to ensure files in the head matches those in the working directory and staging index.
  • -n or –dry-run: It does not remove files but instead tells which files would be removed by executing the rm command.
  • -r: This command allows recursive removal when the leading directory name is given.
  • – -: This option can be used to separate command-line options from the list of files. It is useful when filenames might be mistaken for command-line options.
  • -q –quiet: This option suppresses the output. It normally outputs one line for each removed file.
  • –cached: This option is used only when we have to remove the file from the staging index. Working tree files will be left alone.

Conclusion

We have learned that git revert is a safer option to undo changes instead of deleting commit history. It creates a new commit that inverses the changes and git reset, on the other hand, it is a difficult option for undoing changes. The git rm is used to remove the file from the repository, so these are some of the most commonly used commands for undoing changes. Also to gain better control over the development process of any project it is important to know the reverting of a git commit and other git commands also.

FAQs on How to Revert a Git Commit

Q1. How do I revert a Git Commit on my local branch?

Answer:

To revert a Git commit on your local branch, you can use the following command:

git revert <commit_hash>

Replace <commit_hash> with the hash of the commit you want to revert. Git will create a new commit that undoes the changes from the specified commit.

Q2. Can I revert multiple commits at once?

Answer:

Yes, you can revert multiple commits in one go by specifying a range of commits using the git revert, use this syntax:

git revert <commit_hash_1> <commit_hash_2>

Q3. Can I revert a merge commit?

Answer:

Yes, you can revert a merge commit just like any other commit. Use the git revert command and provide the hash of the merge commit you want to revert.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads