How To Reset Git Head? – POFTUT

How To Reset Git Head?


Git version control tool provides reset functionality in order to clear all uncommitted changes and revert to the last commit. This action is called “reset head”. In this tutorial, we will learn how to Git reset head with different features and examples.

List Changes From The Last Commit

Before resetting to the head we will list current changes in our active branch. We will use git status command which will list all tracked or untracked changes.

$ git status
List Changes From The Last Commit
List Changes From The Last Commit

Hard Reset To Head

Now we will hard reset to the recent commit. The current branch most recent commit is named as HEAD. So this will be called “Hard Reset” to the Head. We will use git reset with the --hard option.

$ git reset --hard HEAD^
Hard Reset To Head
Hard Reset To Head

We can see that HEAD is now at 1ba011937 message means the current branch is hard reset to the latest commit. When we list tracked or untracked changes we can see that all tracked changes are removed and only untracked changes remain.

Soft Reset To Head

Soft reset will reset to the Head but resetting to the head will not delete tracked files. We will provide the --soft option to the reset command.

$ git reset --soft
Soft Reset To Head
Soft Reset To Head

We can see that soft reset does not remove the files extension.c  and pass.c.

Reset Before To Head

Up to now, we have reset to the last recent commit called HEAD. We can specify reset before the last commit or HEAD with the ^ .  For example HEAD^2 means reset to 2 commits before HEAD.

$ git reset --soft HEAD^2

Reset To The Commit with ID

We can also reset to the specified commit ID. This can be useful if we want to reset multiple intermediate commits. We need to provide the commit ID we want to reset. So first we will list commits by their ID’s.

$ git log
Reset To The Commit with ID
Reset To The Commit with ID

Then we will use commit id like below.

$ git reset 350bbe0597d

Reset To Head By Merging

If we want to reset the index and update the files in the working tree that are different between commit and HEAD we can merge them. We will use --merge option like below.

$ git reset --merge

Reset A Single File

We can also reset a single file to the HEAD. we will just provide the file name with the double dash -- option like below. In this example, we will reset the file named portlist.h.

$ git reset -- portlist.h

LEARN MORE  What Is IDE (Integrated Development Environment)?

Leave a Comment