In the fast-paced world of software development, mistakes are inevitable. Whether it's a bug introduced inadvertently or a change in project requirements, there comes a time when roll-backing the last commit becomes necessary. Fortunately, Git, the popular version control system, offers multiple methods to undo changes effectively. In this guide, we'll explore step-by-step instructions on how to revert the last commit in Git using revert, soft reset, and hard reset, along with checking the commit history to ensure a seamless process.
Before proceeding, it's essential to identify the commit you want to revert. Use the git log
command to view the commit history and locate the commit you wish to revert.
git log
The output could be something like this:
The git revert
command creates a new commit that rolls-back the changes made by the specified commit, effectively reverting its effects while preserving the commit history.
It’s important to know that git HEAD
is a reference to the current commit in the repository. It points to the latest commit on the currently checked out branch, representing the snapshot of the project's files at that moment.
git revert HEAD
This command will open a text editor to create a commit message for the revert:
Once you save and exit the editor, Git will create a new commit to revert the changes. This is an output of git log
:
Another approach to revert the last commit is by using git reset --soft
. This command resets the HEAD
to the specified commit, leaving the changes staged and discards the last commit.
git reset --soft HEAD
After executing this command, the changes from the last commit will be staged but not committed. You can then make further modifications or stage/unstage files as needed before committing the changes.
Based on the example above (commit revert), git log
should look like this after the reset:
You can clearly see that the last commit "Revert last commit" is no longer in git history.
Unlike git reset --soft
, git reset --hard
resets the HEAD
to the specified commit and discards any changes made after that commit. Use this command with caution as it permanently removes the changes.
git reset --hard HEAD
Executing this command will revert the repository to the state of the previous commit, deleting any changes made in the last commit.
In the example above, there are 2 commits in git log : "first commit" and "last commit", when executing the hard reset, HEAD
will point to "first commit" and discards last commit:
After reverting the last commit (whether by git revert
or git reset
), it's essential to review the commit history to ensure that the desired changes have been successfully reverted. Use the git log
command again to verify the commit history and ensure that the last commit has been reverted or removed from the log.
In Git, reverting the last commit is a straightforward process, thanks to the versatility of the version control system. Whether you prefer using git revert
, git reset --soft
, or git reset --hard
, Git provides multiple options to cater to different requirements and scenarios. By following the step-by-step instructions outlined in this guide and understanding the implications of each method, you can confidently revert the last commit in Git and maintain a clean and accurate commit history in your projects.