Last Updated on January 30, 2021 by sandeeppote

Git reset allows to specify where the HEAD pointer points to.

HEAD is nothing but with every commit a SHA is created and point to that commit. With Git reset we get flexibility to point to the commit, so any commits after that shall be overridden or ignored based on the option used.

Different ways reset can be performed-

–soft

– is the safest reset and least destructive. It just moves the HEAD pointer but does not change staging index or working directory. Staging and working directory has the later changes and tell the difference with or the changes done in repository.

 git reset --soft SHA1324

–mixed(default)

– this option moves the HEAD pointer to the specified commit and changes the staging index to match the repository but does not change the working directory. All the work in working directory has later changes.

 git reset --mixed SHA1324

–hard

– this option changes staging index and working directory to match the repository and the HEAD is moved to commit specified in command, so any later changes after the commit that is been reset are completely overwritten after the next commit, so use this cautiously.

 git reset --hard SHA1324

After moving the HEAD using -hard option you may still be able to point the HEAD to current commit, so no changes are lost. But if another commit is made after –hard reset then the later changes are lost. For e.g:-

Have following version in git-

V4, V3, V2 and V1 and HEAD points to V4 i.e. the latest commit. Now if you want changes from V2, use following command-

git reset –hard V2

Working directory and staging index will be overwritten, and not if you do git status, HEAD moved to V2. But still the V4 and V3 sits there and now if you want to revert it back to V4 use-

git reset –hard V4

All your later changes are back.

But instead you do another commit V4 and V3 are lost. For e.g.:- if you have again same version’s V4, V3, V2 and V1 and HEAD points to V4

git reset –hard V2

Head is moved to V2 and staging and working directory is changed, now make changes to any file,

git add file.txt

git commit -m “reset the head and changes to the file”

the version in repository shall be as follwos-

V5, V2 and V1

so in this case V4 and V3 are completely lost.

 

Hope this helps 🙂