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 as “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.

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 as Hard reset to the Head. We will use git reset with the --hard option.

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.

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.

LEARN MORE  What Is IDE (Integrated Development Environment)?

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.

Reset To The Commit with ID

Reset To The Commit with ID

Then we will use commit id like below.

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.

Reset A Single File

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)