Git revert is the easiest way of undoing the commits early changes or commits that are faulty in a Git environment. This situation might happen when users are dealing with a collaborated development project that already exists in the server with a log history. The “git revert” command can be used to undo the added changes by reverting the commit. Moreover, the revert operation is useful while dealing with bugs.
This post will describe the procedure of reverting a range of commits in Git.
How to Revert a Range of Commits in Git?
To revert the range of Git commits, check out the following institutions:
-
- Redirect to the Git root directory.
- Check the commit reference log history.
- Select the range of commits from the history and copy their start and ending commit SHA-hash.
- Execute the “git revert <start-commit-hash>…<end-commit-hash>” command.
- Verify it by displaying the log history.
Step 1: Move to Git Root Directory
At first, type out the “cd” command with the Git root directory path and switch to it:
Step 2: View Log History
Next, execute the provided command along with the desired number of a range that needs to display the Git reference log history:
In the below-provided output, the highlighted commits are our target commits which we want to revert. For this purpose, we have copied the start and target commit SHA-hash:
Step 3: Revert Range of Commits
Run the “git revert” command to revert the multiple commits simultaneously:
After executing the above-provided command, the “COMMIT_EDITMSG” file will be opened with the default editor. Now, add new commit messages against the selected commits one by one, which need to revert. Add commit message, save changes, and close the editor:
As you can see in the below-given output, the range of commits have been reverted:
Step 4: Ensure Revert Operation
To verify if the selected number of commits are reverted or not, execute the following command:
It can be observed that all below-highlighted commits are reverted successfully:
You have learned the easiest method of reverting multiple commits at once in Git.
Conclusion
To revert the range of Git commits, first, move to the Git root directory and check the commit reference log history. Then, select the range of commits from the history and copy their start and ending commit SHA-hash. After that, execute the “git revert <start-commit-hash>…<end-commit-hash>” command and verify it by checking the log history. This post demonstrated the way of reverting a range of commits in Git.