Introduction
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.
Prerequisites
- 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:
git log
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 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 more about Git checkout tags, how they are useful for your development project and how to use them.
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 stash
command:
git stash
git reset --hard [hash]
git stash pop
The 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 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).
Verify the status by entering:
git status
The current commit is running in detached head status
To fix it, display the hash of the new (reverted) commit:
git log
You now see the hash of the new commit and the reverted commit as well.
To view the latest (reverted) hash:
git checkout [hash]
Conclusion
You now know how to use the revert
and 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.
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/tech/dev/223217.html