Can we reset HEAD without unstaging?

Question

We learn in this lesson that git reset HEAD filename is a useful way to unstage filename and update the HEAD pointer. Is there a way to simply update the HEAD pointer while keeping the stage as is?

Answer

The short answer is yes but let’s first justify wanting to move the HEAD and leave the staging area the same. One reason you may want to rollback the HEAD without clearing the staging area is to commit the files currently in the staging area immediately (or soon) after the rollback.

With that in mind, how do we do this? The way we accomplish this is by writing

git reset --soft HEAD

or if you have the commit SHA hash,

git reset --soft commitSHA

So the key is to use reset with the argument --soft.

2 Likes

What is the HEAD pointer? Thank you.

8 Likes

As far as I understand it: HEAD is a variable that means “the thing I am currently working on.” Usually this is a branch - the most recent commit on that branch - although you can also move HEAD further back in the commit history to some earlier commit.

Say your project has a branch main and a branch side, and you’re working in main. HEAD is currently pointing to main. You can git checkout side to move to side, and this updates HEAD to side.

Here’s another brief explanation.

10 Likes

The way I think of HEAD is as a “You are here” sign. It can be moved to point to different branches, which I think of as bookmarks. If you move it to a specific commit using the commit’s hash, it’s called a detached HEAD, and any commits you make won’t be bookmarked by a branch :frowning:

1 Like

I don’t think I fully understand the point of reset HEAD. We are removing the changes from the staging area, but the file itself still has the changes applied. Whats the difference between staging a file, then unstaging it to make changes and then staging the file again versus staging file, making the proper changes to the file and then staging again ?

I have this question too