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
How to View Last Commit
Git offers many features to manage your project from different historical commits. For example, you can view an old commit, then create a branch from it.
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.
Examine Previous Commit
To analyze the status of your project from a previous commit, use the
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 Git checkout tags and why they are useful for your development project.
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 the
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.
Revert Published Commits
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. Other developers may have retrieved the updated project already.
Deleting updates from one system may cause conflicts with other team members. Instead, use the
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).
Verify the status by entering:
The current commit is running in detached head status
To fix it, display the hash of the new (reverted) commit:
You now see the hash of the new commit and the reverted commit as well.
To view the latest (reverted) hash:
git checkout [hash]
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.