github linkedin rss
Git reset
Jun 26, 2014

In this post I will go through the git reset command - when you might want to use it, and when it might not be the optimal choice.

What does it do?

The simple description of git reset goes something like this: git reset is a command which can be used to undo changes. I don’t think that this description encapsulates the possibilites of the command though.

I think that the most common usage of git reset is undoing changes to the staging area or working directory. But you could also use if for undoing a series of commits.

Please do note that it isn’t recommended to use git reset on commits which are pushed to a collaborative repository. To be on the safe side, only use the command on local work.

How do I use it?

How to use the command depends on what you want to accomplish of course. But I thought that I’d show you a couple of different ways to use it in the following examples.

Unstaging files

A simple and common use case of the git reset command is unstaging files. Lets say that you are working on a new feature for a project. You have modified and added two files, index.html and login.html, to the staging area when you realize that you only want one of the files to go into the next commit.

When you run the git status command you get the following output:

On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

    modified:   index.html
    modified:   login.html

Just as the output of git status states, you can use git reset HEAD <file> to unstage a single file. If you want to unstage both of the files you could use git reset.

By default git runs the command in the mixed mode, so entering git reset is the same as git reset --mixed. The mixed mode removes files from the staging area but keeps the changes.

After deciding that you want only login.html to go into the next commit you enter git reset HEAD index.html, which removes it from the staging area. Entering git status you now receive the following output:

On branch master
Changes to be committed:
  ...
    modified:   login.html

Changes not staged for commit:
  ...
    modified:   index.html

Undoing changes

As stated in the above example, git uses the mixed mode by default - which just unstages files. But what if the case is that we want to remove the changes we’ve done since that last commit? Enter hard mode!

Trivia: Hardcore git users often say “go hard mode, or go soft”. Ohh, sorry I lied.

Lets say that you are working on the same project as before. You feel that you’re latest changes are completely unnecessary and that you’d like to start on a clean slate. Executing git status gives the following output:

On branch master
Changes to be committed:
  ...
    modified:   login.html

Changes not staged for commit:
  ...
    modified:   index.html

Executing git reset --hard undoes the changes you’ve introduced since your last commit. Unless you’ve created a new file which is not yet tracked by git that is, then that one will still be kept. git status now gives you:

On branch master
nothing to commit, working directory clean

Undoing commits

The last example will show how to undo a series of commits. Which you would like to use when… well, when you want to undo a series of commits I guess. It could be as in the earlier case, when you feel that the work you’ve done in the most recent commits isn’t worth saving.

In this example the soft mode is used. The soft mode will actually keep all your changes - both changes that you have staged, and changes in your working directory (unstaged).

The command git log --oneline shows your commit history:

4ecc068 Work on index.html
1408dd9 Work on login.html
4ac94bb Create login.html
2ea1a4c Create index.html

You decide that you would like to undo the two latest commits, and change the commit messages (or something along those lines). You therefore enter the command git reset --soft 4ac94bb. If you run git log --oneline again it will output:

4ac94bb Create login.html
2ea1a4c Create index.html

And a git status will give you:

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   index.html
    modified:   login.html

Feel free to commit these files again or whatever you wish for!

Further reading


Back to posts