Git is a source code versioning or management tool which is used to create multiple versions of different branches of the source code and software. In some cases, we may need to revert or reset changes to the previous commit which is the HEAD of the branch. By resetting the current branches errors or mistakes can be removed easily. In this tutorial, we examine how to reset to head in Git.
Git Hard Reset To HEAD
There are two types of reset. A hard and soft reset is used to reset into the specified commit like HEAD. The hard reset simply removes all files after the HEAD commit. The
git reset --hard HEAD command is used to hard reset to HEAD.
$ git reset --hard HEAD
Git Hard Reset To Commit Before HEAD
Sometimes we may need to hard reset before the HEAD commit. Some descriptors are added to the HEAD statement to specify the previous commits. Use the following command to hard reset to commit before HEAD.
$ git reset --hard HEAD~1
Git Soft Reset To HEAD
Git can be also used to soft reset to the HEAD. The soft reset does not alter the working directory and the index. The following command is used for the soft reset to HEAD.
$ git reset --soft HEAD~1