Removing files from the last commit

We’ve all been guilty of doing a lazy git commit -a, accidentally including a file we didn’t mean to in the commit. Or perhaps you meant to at the time, but later decided to split it into two commits. You can easily fix this with git reset.

TL;DR

I know you’re busy, so here’s the quick and dirty version:

$ git reset --soft HEAD^
$ git reset HEAD folder/file.js
$ git commit -c ORIG_HEAD

Now for a little explanation

The first thing we want to do is get our staging area back into the state it was just before we made the commit. Whenever you see the caret symbol (^) it means the commit just before commit it‘s attached to. In this case HEAD represents the most recent commit, so we want to reset to the commit before the most recent.

git reset --soft HEAD^
git reset HEAD folder/file.js
git commit -c ORIG_HEAD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store