How to Fix a Bad Git Commit

Written by: on August 16, 2016

Have you ever found yourself hacking away on a new feature when you realize you’ve been committing to the wrong branch? Have you made a commit and then realized you forgot to stage a file? Let’s explore some of the Git tools we use to recover from these situations.

Committing to the wrong branch can be frustrating. When working on multiple tasks, it’s easy to forget to switch branches. There are a couple methods that can be used to get a commit to the right place. However, as with anything that edits your git history, I strongly recommend making a spare branch as a backup before continuing. This can be done through the command line with, ‘git branch backup’ to make a backup just in case something goes wrong.

Before doing anything our branch should look something like this:


The easiest way to undo a commit is git reset. Take note of the commit hash of the parent commit of your bad commit. Type git reset –soft to point your current branch to the commit that branch should be pointing to. Typing git status shows all the files that were changed in the deleted commits.


Type git stash to stash the changeset and checkout the branch those changes should really be apart of, then git stash pop to apply. Fix any conflicts if needed and commit.


Another common mistake is making a commit that isn’t quite finished. Maybe you forgot to stage a file, or you botched the commit message. Perhaps you forgot to sign your commit. Whatever the case, the fix here is the same, provided you have not already pushed the commit elsewhere. The git commit command has a handy flag ‘- -amend’. The amend flag does exactly as it sounds. It works just like a normal commit command but updates the commit you are currently pointed to. This is a good way to fix messages, signatures, missing files, and a number of other things that can be incorrectly formed on a single commit. The downside is the – – amend flag edits your Git history, so only do this if you have not pushed your changes elsewhere.

Eric Young

Eric Young

Eric is a developer at POSSIBLE Mobile. Eric develops Android apps by day, hacks on Rust programs by night, and constantly tries to find ways to make his code more maintainable.

Add your voice to the discussion: