Our team moved to git recently, and there’s a lot of talk about how confusing “reset” is. If you don’t understand the ins and outs of reset, Scott Chacon’s Reset Demystified is a must read. His visualization of the “three trees of git” is the best I’ve read, and once you’re able to visualize each “tree”, reset is a lot less intimidating. In fact, it’s downright simple.

I strongly suggest reading the article, because reset is powerful and understanding how it works is much better than only knowing a few tricks. If you’re in a hurry, though, here’s an easy way to remember what reset is doing:

git reset –soft HEAD^ Undo last commit git reset –mixed HEAD^ Undo last commit AND unstage files git reset –hard HEAD^ Undo last commit AND unstage files AND undo changes in working directory

Replace HEAD^ with any commit, and you can jump around anywhere in the commit graph. This is very powerful, but can be a little frightening if you came from SVN. I recommend you experiment with each command and see for yourself what they do, as reading about it is no substitute for actually doing it.

Hop on to your nearest repo, create a new branch with “git checkout -b resetsandbox”, and start playing around. See if you can do all of the following for yourself using only “git reset”:

There was a bug 2 commits ago. Go back in time so you can reproduce the bug in your working directory. Go back 5 commits in the tree, but keep all of the changes around so you can re-commit them as one big change (kind of like a manual –squash). Change a file in your working directory, then add it to the index with “git add”. Now un-stage that change, using only “git reset”, but don’t destroy your change in the working directory. Go back to where the branch was before we made all of those changes so the branch is a clean copy without all of that sandbox stuff. (Hint: use “git reflog” to find the commit you need)

If you run into trouble, drop a comment on this post or ask me.