Home » How To » How to Git Reset to Head

How to Git Reset to Head

In this tutorial, we will learn How to Git Reset to Head?

Git reset is a process that is pretty similar to undoing the recent Git commit that we covered in one of the previous tutorials. However, in this one, we will cover Git reset to Head in more depth. We will check what the revert command does and what is mixed reset. Read on and find some tips and tricks about Git reset.

Reset Last Git Commit to HEAD

In our previous article, we used git reset –soft HEAD~1 to undo the last commit without losing changes that were uncommitted. Additionally, we used git reset –hard HEAD~1 to undo everything, even changes that we made locally. But what to do when you want to reset the last Git commit to HEAD, keep the changes that you did in your repo directory, but you don’t want to keep them in the index? Here’s your answer.

Related:  Git Cheat Sheet

If you stumble upon situations like the one that we described above, you have to use –mixed flag. Here’s an example.

Let’s say that we added some sort of file with our last commit.

git log --oneline --graph 
Output:
d445900 (HEAD -> master) Added a new file named "test_file"
61t6ll5 Second commit
4096r12 Initial repository commit

Now let’s run Git reset command with --mixed flag.

git reset --mixed HEAD~1 

What the command above did is the following. It removed the last commit, which in this case was file addition and it removed it from the Git Index, but the file remained in the directory where you are currently located ( which is your local repository directory ). So flag --mixed is actually a combination of --soft and --hard Git reset options. That is why it’s called mixed in the end.

How to Use Git Revert Option to Reset

Revert is a bit different than reset. The main difference is that reset sets a new position for HEAD while revert actually reverts the whole commit which is specified. Let us show you an example of how this actually works.

git log --oneline --graph 
Output:
d445900 (HEAD -> master) Added a new file named "test_file"
61t6ll5 Second commit
4096r12 Initial repository commit

So again, the last thing that we committed was file addition. Let’s run the revert command now.

git revert HEAD 

Your default text editor will open now and the output will look like this.

Revert “Added a new file named test_file”

This reverts commit 5e998t74du5h4z4f.

# Please enter the commit message for your changes. Lines starting
# with ‘#’ will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is ahead of ‘origin/master’ by 6 commits.
#    (use “git push” to publish your loacl commits)
#
# Changes to be committed:
#                deleted:         test_file
#

Once you are done, exit the text editor, and a new message will pop up.

Output:
[master d445900] Revert "Added a new file named test_file"
 1 file changed, 1 deletion(-)
 delete mode 100644 test_file

That’s it! You successfully competed Git reset to HEAD action with the revert option.

Leave a Comment