How to use Git Reset to HEAD

Tram Ho

When working on a multi-member project, team members can create branches, add, edit, and delete files in the project. Then make commits to git when the code is done. However, in some cases, you may find that the changes you’ve made aren’t going so well.

You have modified some files, added and removed a lot of lines from your files, but you want to go back. Because of that, you want to “revert the changes” what you did and go back to the files you had originally.

This technique is called “reset to HEAD” and it is quite a powerful tool for developers to use.

In this article, I will show you how to use reset to HEAD on Git.

1. Git Hard Reset to HEAD

When resetting files on Git, we have 2 options: hard reset files and soft reset files.
In this section, I will describe how you can hard reset files on Git.
To factory reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD.

Example of Hard Reset

To understand “factory reset” use cases, check out some quick examples below.
When trying to reset files, the first command you want to launch is the “git log” command.
Using the “git log” command, you will be able to get an overview of your current Git branches and its commits.

As you can see the commit with HEAD 941c450 is the last commit in branch develop
To factory reset before HEAD use “git reset” with “–hard” option and specify HEAD ^.

As you can see, the HEAD of the release branch is now pointing to the second commit: we’ve basically reset to the commit before HEAD.

Factory reset for HEAD

To undo a factory reset on Git, use the “git reset” command with the “–hard” option and specify “HEAD @ {1}”
Using the example we used before, that will give us the following result

2.Git Soft Reset to HEAD

To restore files to HEAD on Git, use the “git reset” command with the “–soft” option and specify the HEAD

In contrast to hard reset files, soft reset files will not change the working directory and index.
Therefore, changes made between the original HEAD and the current HEAD will be implemented.
Going back to the example we took earlier, let’s take a quick look at the “develop” branch.

To move HEAD to a previous commit, use the “git reset” command with the “–soft” option and specify “HEAD ^”

160 / 5000

Translation results

This time, the staging area will be filled with the changes made between commit 8cfada9 and commit 941c450. See the changes with the “git status” command.

Combining commits using soft reset

A common use of the soft reset command is to combine many different commits into a single commit.
On your current branch, see all the commits that are currently made.

To combine the last three commits, move the HEAD using the “git reset” command with the “–soft” option.

Now that the commits are rolled back, commit the files with the “git commit” command.

Your commits are now combined in a single commit.

Note: In case you have made commits and pushed the code to the server, then I will execute the following commands in turn to update the commits on the server:

Hope this article will help you better understand how to use Git Reset HEAD!

Share the news now