Git is a decentralized version control system utilized among developers working on similar projects as a team. On this platform, the team members can make changes at any time and inform other members by committing changes from the local repository to Git remote repository. However, sometimes, one can mistakenly commit and want to remove it. In such a scenario, Git allows its users to revert multiple commits simultaneously.
This study will discuss the procedure of reverting multiple commits in Git.
How to Revert Multiple Commits in Git?
To understand the procedure of reverting multiple commits, first, move to the specific directory. Next, create and add files to the Git directory. Then, revert multiple commits.
Now, go ahead and perform the specified instructions.
Step 1: Launch Git Bash
Search and launch the “Git Bash” terminal using the “Startup” menu:
Step 2: Navigate to Git Directory
Move to the specific directory where you want to revert multiple commits:
Step 3: Create New File
Next, create a new file utilizing the “touch” command:
The error-free output indicates that the new file “file3.txt” is created successfully:
Step 4: Add File into Git Repo
Now, add the created untracked file to Git repository:
Step 5: Commit Changes
Next, execute the “git commit” command with “-m” flag to commit all changes:
Step 6: Create New File
Run the “touch” command to create a new file:
Step 7: Add File
Add the newly created file into the Git repo utilizing provided command:
Step 8: Commit Changes
Now, commit all changes to Git repo with a message:
Step 9: Check Log History
To see the current position of HEAD, check the log history utilizing the “git log” command with the “–oneline” flag:
As you can see, we have more than one commit, and currently HEAD is referring to the most recent commit:
Step 10: Revert Multiple Commits
Execute the “git reset” command with “–hard” option to revert multiple commits together:
Below output indicates that our HEAD is successfully reverted to “1ec268c” commit ref which is our first commit “Create file1” and multiples commits are removed:
Step 11: Check Log History
Lastly, execute the “git log –oneline” command to verify the performed operation:
Here, our “first_demo” directory contains just one commit rest of the changes are removed:
That’s it! We have provided the easiest way to revert multiple commits in Git.
Conclusion
To revert multiple commits in Git, first, open the Git terminal, and navigate to the specific Git directory in which you are required to revert multiple changes. Then, create and add files to the directory. After that, commit all changes. Lastly, run the “$ git reset” command with the “–hard” flag and the commit reference to revert them back. This study has elaborated on the procedure of reverting multiple commits in Git.