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.
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^
The next thing we want to do is move any files we don’t want to be included in the commit out of the staging area. When run without --soft flag, git rest defaults to the --mixed flag which means “the changed files are preserved but not marked for commit.”
git reset HEAD folder/file.js
Now you can commit the staged files again with a git commit, or you can commit them with the original commit message (you’ll still have a chance to edit the message).
git commit -c ORIG_HEAD
Warning: If you’ve already pushed your changes to a remote, you will have to push with -f after modifying the commit. If someone has already pulled your changes, this could potentially cause problems.