When working on a project, sometimes you might have to reset your repository to a previous commit. In this post, we will look at the three types of resets that can be used to reset a repository to a certain commit: soft, hard and mixed.
Before using any of these commands below, make sure that you have backed up your work.
Make sure that you are in the branch that you want to reset (
git branchto check what branch you are currently on). If you have to switch branches, type:
git checkout nameOfBranch.
Copy the SHA1 hash of the commit that you want to reset to and replace
<SHA1>with the hash that you copied.
Soft resetting a commit
Soft resetting only resets the pointer. It leaves all your changed files that have been staged as is. This is considered a safer choice in comparison to hard resetting. In order to do a soft reset, type the following:
git reset --soft <SHA1>
Hard resetting a commit
Hard resetting will replace any local changes that are uncommitted with the commit that you just reset to and clears anything that has been staged. This command should only be used only when necessary. In order to do a hard reset, type the following:
git reset --hard <SHA1>
Mixed resetting a commit
Mixed resetting is considered the default mode for git reset. It resets the pointer (soft reset) and clears anything that has been staged (hard reset), but local changes that are uncommitted are kept as is. In order to do a mixed reset, type the following:
git reset --mixed <SHA1>
--mixed parameter is optional in this case since it is the default mode for
reset. You can omit it if you wish.