`git reset`
| 4 minute read
Have you ever commited something unintentionally? If you have, you might reach for git reset
to roll your history back.
Roll back and keep your changes
To keep your changes you'll use the --soft
flag.
The following will roll back one commit from your current state.
$ git reset --soft HEAD~1
Roll back and discard your changes
If you never want to see that code again you'll use the --hard
flag. There is no undo for this, so be careful! I've definitely learned this lesson the hard way.
The following will roll back one commit from your current state and discard all changes.
$ git reset --hard HEAD~1
To reset your HEAD to a specific commit
If you are looking to roll back to a specific commit you will pass the id of that commit. The commit you pass will become the new HEAD
of your tree.
$ git reset --soft 32eccb1
Learn more
There is a lot more that git reset
can do for you. You can find the docs here if you want to know more.
If you are just looking to edit the commit itself and not roll your history back you might be looking for git commit --amend
. The docs for this can be found here.