Say you are working on a git branch and have a chain of commits lined up.

gitGraph
    commit id: "update deps"
    commit id: "docs v1"

    branch feature
    checkout feature
    commit id: "add feature"
    commit id: "bug fix"
    commit id: "faulty commit"
    commit id: "enhancement"
    commit id: "docs v2"

Now say you realize commit id faulty commit needs to be removed, this can be verified maybe using git bisect start command.

But say you know that faulty commit is the commit to be removed, we can do it in the following manner using interactive rebase:

git rebase -i <faulty-commit-hash>^

The ^ tells Git to use the parent of <faulty-commit-hash> (which happens to be bug fix) as the starting point for the rebase. This ensures that the target commit is included in the interactive rebase-todo-list. Running this command should open an editor and you’ll have the following todo list:

pick 940b012 # bug fix
pick fa61a6e # faulty commit
pick e588d5e # enhancement
pick b1cd783 # docs v2

Just edit this document and replace pick with drop for the commit id faulty commit. Save it and close it.

pick 940b012 # bug fix
drop fa61a6e # faulty commit
pick e588d5e # enhancement
pick b1cd783 # docs v2

You might need to resolve any merge conflicts. And now you’ll see the commit from this branch history and git will rewrite the history with new commit hashes.