Git

How to Revert a Range of Commits in Git

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:

$ cd "C:\Users\nazma\Git"

 
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:

$ git log --oneline -10

 
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:

$ git revert 90c1f00...37ecab4

 
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:

$ git log --oneline -10

 
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.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.