}

How to undo last commit not pushed?

Created:

Introduction

Usually developers avoids doing a big commit with multiple features or bug fixes. If you do small commits with only a fix, it will be easy in the future to revert or detect an error (for example when using bisect). Sometimes when a developer finishes a future it could be the case that he or she also fixes a bug which is not related to the feature, but if he/she was in a rush it's a common mistake to add every change to the commit. In this short tutorial we will see how to fix a commit not yet pushed to the remote repo. In this tutorial we are going to remove changes from the commit, if you forgot to add something use git commit --amend

If you pushed the commit to the repo, we recommend to undo the unwanted changes with another commit. Most of the repositories are configured to not allow history changes, anyways if the repo allows history changes we don't recommend to rewrite the history undoing commits on the remote repo.

First Step

git reset HEAD~

git reset HEAD~: Will do soft reset, which means it will leave your files on disk unchanged but undoes the commit. Now you have all your last commit in the unstaged state.

Second Step

At this step we need to fix staged files to fix the commit. I recommend to do a reset HEAD and the use the git add -p Now you can do a git reset HEAD to unstage all the files in that wrong commit. To easily add blocks of changes use the git add -p. When you have all the block of changes ready, do the commit like always.

Here optionally you can use git commit -c ORIG_HEAD to avoid rewritting the old commit message.

Now you are ready and everything should be fixed as expected.