Git is a popular project tracking application. As a project progresses through different revisions, those revisions are published as a commit. A commit represents a checkpoint, where a copy of the project is saved at a specific point.
This guide shows you how to undo the last commit in git.
- An existing project in Git
- Access to a terminal window/command line
Note: If you wish to undo a commit to correct an error in the commit message, there is a better way to do it. Learn how to change a Git commit message.
How to Revert Commit
Git offers many features to manage your project from different historical commits. You can view and revert a previous commit, either published or unpublished.
Read on to learn how to identify and revert a published commit. Also, if you want to learn more about Git, check out our article on how does Git work.
Identifying the Commit
A hash is an alphanumeric code that identifies each commit. Enter the following command to display a commit hash:
The hash is necessary to display or manage a specific commit.
To analyze the status of your project from a previous commit, use the checkout command:
git checkout [hash]
When using a hash with a Git command, there is no need to type it in its entirety. The first few unique characters are enough for Git to identify an entry accurately.
Note: Find out how to use Git checkout tags and why they are useful for your development project.
Git Revert: Reverting the Last Git Commit
Once a commit is uploaded to the server, it creates a more permanent project log. It is not advisable to use reset in this case as other developers may have retrieved the updated project already.
Deleting updates from one system may cause conflicts with other team members. Instead, use the revert command:
git revert [hash]
Make sure to enter the code for the hash you want to revert to. The system asks you to enter a specific commit message for the changes the revert command is going to perform.
This action creates a new commit based on the one you specified, with a
revert tag. This acts as a log, showing that the commit was published and then reverted (instead of pretending it never happened).
Note: Learn how to use Git cherry-pick to select and apply a single commit from one branch to another.
Git Reset: Revert Unpublished Commits
An unpublished commit is an update committed in Git but that has not been uploaded to a server. To reset to a previous commit, before any changes were made:
git reset --hard [hash]
This command wipes the slate clean back to the previous commit. Any changes you made will be lost after using the
reset --hard command.
If you want to preserve your work, you can use Git stash:
git reset --hard [hash]
git stash pop
stash command saves the work you did, and
stash pop retrieves those changes after the reset. Alternately you can use the following:
git reset --soft [hash]
This command resets the commit history, but it leaves your working directory and staging index as-is.
Note: Git stash allows users to save their uncommitted files and work in progress to a local stash. Learn how to Git stash specific files.
Git Amend: Modify the Last Commit
To modify the most recent commit, use the
git commit --amend command. This command replaces the last commit with the amended commit without altering the snapshot.
--amend flag is usually used to fix minor mistakes.
For instance, you want to commit a couple of files in a single snapshot but forget to add one of the files before committing. This issue is easily solved using the
First, you add the missing file using:
git add file2.py
Then, run the command:
git commit --amend --no-edit
--no-edit option amends the commit without changing the commit message.
Note: If you need to restore a missing repository, refer to our guide How to Restore a Git Repository.
You now know how to use the
reset commands to undo changes and revert to a previous commit. Additionally, you have learned how to check the status of your current commit.
Remember not to use
reset for published commits as it may lead to version conflicts.