3 Common Git Scenarios and How to Deal With Them
Tags: Git, Software Development, Programming, Github, Software Engineering

Overwrite Git Commit History

I have had to overwrite my remote branch commit history a few times in the past. The main reason I had to do this was because of unused or bad commits I accidentally or unknowingly had made, only to realise after that those commits weren’t needed.

This can also happen when the requirements change, which I can only assume happens a lot anywhere, anytime. As developers, we like to keep things tidy and clean, so we tend to remove what is not necessary (at least I do that).

Without any further ado, let’s see how we can clean up our commit history.

To start with, let’s pretend that we have only one commit (the latest one) we want to erase from the history.

Reset tree to the commit prior to latest

We can rewind history to the commit prior to the latest commit that is currently in local and remote:

$ git reset HEAD^

Note that this is known as the soft reset, which means the changes in the latest commit we want to remove from the history are preserved. This is useful in case you want to use some of the changes.

If you do not want to use any of the changes from the latest commit at all, then you can do what is called a hard reset. Please be warned that a hard reset does not preserve the changes from the latest commit. In other words, they’re gone for good.

$ git reset --hard HEAD^

Remove the latest commit in remote

What we’ve done so far is just reset or remove the latest commit locally. If the latest commit was already pushed remotely, we want to remove that as well:

$ git push -f

The -f option is also known as git push --force , which means it will copy all the commits from the local branch and push them all to the remote counterpart, removing all commits that are not in the local branch.

In other situations, we might have multiple commits we want to remove from the history. The steps are quite similar to removing just the latest commit. Let’s go through them.

Reset tree to a specific commit

Git logs all commits that have been made and each of them has a unique commit ID. To get the list of commits, we can do this:

$ git log

The output looks something like this:



Author: billydh <

Date: Mon Mar 23 20:58:10 2020 +1100 commit e1cc9ff850d36adc59a50c30685fb1d9414fd9e4 (HEAD -> master, origin/master)Author: billydh < someemail@gmail.com Date: Mon Mar 23 20:58:10 2020 +1100 use then instead of flatMap when commiting

Author: billydh <

Date: Mon Mar 2 21:05:00 2020 +1100 commit 8d4edb1b952cca32bebfefacb90307250a6c9891Author: billydh < someemail@gmail.com Date: Mon Mar 2 21:05:00 2020 +1100 update readme

Author: billydh <

Date: Mon Mar 2 21:01:06 2020 +1100 commit 80985dd486b6af62a649b89626891ac571258146Author: billydh < someemail@gmail.com Date: Mon Mar 2 21:01:06 2020 +1100 restructure project

Let’s say we want to remove the last two commits. This is how we can do that:

$ git reset 80985dd486b6af62a649b89626891ac571258146

A reminder here is that git reset by default does the soft reset, meaning all the changes are kept. If you want to delete them altogether, you can always add the --hard option to the git reset command.

Here’s another way to remove the last two commits or n commits:

$ git reset HEAD~2 # replace 2 with `n` last commits you want to remove

Remove the latest commit in remote

Finally, we also need to do the git push force to overwrite history in the remote branch:

$ git push -f

That’s all you’ve got to do.