How To Git Reset Hard?


git reset or git-reset commands are used to reset changes to the previous version in Git software. Git reset is popularly used because of its function where we can go back to whatever version we want. Reverting changes to the back can be done in different ways. “Git reset hard” is a way by deleting specified changes permanently and can not bring back.

Hard Reset vs Soft Reset

There is two reset methods named as Hard Reset and Soft Reset. Hard Reset will remove all the working directory and staging are files and index. But the Soft Reset will not alter the working directory and the index. This means in the Soft Reset the changed between original HEAD and the current HEAD will be staged.

List Commits

Before hard reset, we may need to list the commits in order to get more reliable information because the hard reset will delete persistently and there is no way to get back deleted files. We can list last commits with the git log command. Also, extra options can be specified to change the commit list format.

$ git log

$ git log --oneline 

$ git log --graph

$ git log --oneline --graph
List Commits

Hard Reset To The Head

One of the most used git hard reset operation is resetting to the head. Developers generally makes some changes in the HEAD and then do not like and reset back to the HEAD. As hard reset will remove all changes hard reset to the HEAD commit is used. Keep in mint that the following command will remove all changes and the changes can not be bring back.

$ git reset --hard HEAD

Hard Reset To The Right Before Head

Well in some cases we may need to hard reset to the previous commit before the HEAD. We can use the ^ sign in order to specify the previous commit before HEAD and use the git reset --hard command like below.

$ git reset --hard HEAD^

Hard Reset To The Specified Number of Commits Before Head

Also we can specify the commit count before the HEAD in order to reset. We will use the tilda sign ~ and the number of commits we want to reset back from the HEAD. In the following example we will hard reset 8 commits before HEAD.

$ git reset --hard HEAD~8

Hard Reset To The Specific Commit

Hard reset can be done without HEAD related commit specification. We can hard reset to the specific commit by providing the commit hash like below.

$ git reset --hard 29c6a536042915c8ed49

LEARN MORE  How To Reset Git Head?

Leave a Comment