Git Reset

Git Reset

The git reset command is a very power and very dangerous command. It allows you to move the HEAD pointer to any position. You could for example, move the head power to any previous commit(rewind). Once you perform a commit(recording) all the commits that came after HEAD will be lost forever.

Types Of Reset

There are 3 types of reset.
  1. --soft - Only moves the head pointer and does nothing else. Does not change the staging index or the working directory. Considered the safest. After a --soft reset the repository will be set back to an earlier state. The index and working copy will still have the latest code. A diff between the with the working copy or the staging index will show that.
  2. --mixed(default) - Moves the head pointer. Changes staging index to match new repository state. does not change your working directory.
  3. --hard - changes the staging index and working directory to match the new repository states. This means, any change made since the reset point will be completely lost, forever.(Most Dangerous).

Git reset --soft

Take the following as an example:

To reset the respository to the state it was at after the first commit. Run the following
git reset --soft 9555ccb1c414158bed

The working directory and the staging index remain unaffacted by the fact that the repository HEAD has been changed.  The staging index appears as if a.txt was modified. This is because the staging index no longer matches the repository. Both the working directory and the staging index retain the updates of the second commit. However the respository itself only contain the first commit. We can undo the soft reset by moving the pointer head back to the second commit. git reset --soft 9bf6aea93eaf50d8

Git reset --mixed

Resets the staging index and the repository to a previous commit state.
git reset --mixed 9555ccb1c414158be
Both the respository and the staging index will be reset to the first commit. However, the commit from the second directory will not be lost. Our working directory still has the changes from the second commit. git status will show the a.txt as modified because our working directory no longer matches the staging index.

We can undo the mixed reset by moving the pointer head back to the second commit. git reset --mixed 9bf6aea93eaf50d8

Git reset --hard

This is a destructive reset. It gets rid of the changes in our working directory, staging index and repository and sets everything back to the state of the first commit.
git reset --hard 9555ccb1c414158
A git status will show nothing to commit.

If we haven't made any other commits yet we can go back to the second commit with git reset --hard 9bf6aea93eaf50d8
Cookbook Category: 

Add new comment