logo

Best ways to remove commits from git

Mastering git is like driving a sports car, even if you make a wrong turn you can navigate back to your way quickly. Knowing how to fix common git issues can speed up you development process several folds.

We all have taken wrong turns

We have all been there when we made a commit to a wrong branch or added wrong file to a commit. There are several way to fix the problem but sometimes some method are better then other. This article will show you 4 different ways to remove those commits.

There are different methods to remove commits suited for different needs, for example if you’ve already pushed the code to upsteam and the code is being used by other developers you can’t force push the changes in that case the best option is to use git revert command, If the commit is the most recent one then *git reset *can make our work a lot simpler.

1. git revert

This option is most suitable if commit is already pushed code is being used by other developers.

A revert operation will take the specified commit, inverse the changes from that commit, and create a new “revert commit”. The ref pointers are then updated to point at the new revert commit making it the tip of the branch.

git revert <hash>

2. git reset

If there are no commit on top of the commit which you wanna remove(or you wanna remove all commit upto that point) and you haven’t pushed the code yet then git reset can save your day.

You can think of it as a “rollback” — it points your local environment back to a previous commit

git reset HEAD~N

Here N is the number of commits you wanna remove.

There are 3 options in git reset

  1. git reset — — hard

  2. git reset — — mixed

  3. git reset — — soft

Hard option will completely remove the commit (dangerous), Mixed is default option and will move the content of commit to unstaged area, **Soft **will move the content of commit to staged area.

If this code is already pushed you need to use *git push -f *to force push it to the upstream.

3. git rebase

It is one of the most helpful commands out there, it allows to modify older commits, you can also use git rebase to combine commits into a new commit. git rebase allows you to rewrite history. Since you will be modifying the history it’s important that you’ve not pushed upstream or no one is working on that branch.

git rebase -i HEAD~N

Where N is the commit-number of commit you wanna remove.The -i flag shows that its an interactive rebase

You will se something like this

    pick s3f03mw commit 2
    pick 734x3fe i want to remove this commit
    pick 45ff26c commit 4

    # Rebase e851499..45ff26c onto e851499 (1 command)
    #
    # Commands:
    # p, pick <commit> = use commit
    # r, reword <commit> = use commit, but edit the commit message
    # e, edit <commit> = use commit, but stop for amending
    # s, squash <commit> = use commit, but meld into previous commit
    # f, fixup <commit> = like “squash”, but discard this commit’s log message
    # x, exec <command> = run command (the rest of the line) using shell
    # b, break = stop here (continue rebase later with ‘git rebase — continue’)
    # d, drop <commit> = remove commit
    # l, label <label> = label current HEAD with a name
    # t, reset <label> = reset HEAD to a label
    # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
    # . create a merge commit using the original merge commit’s
    # . message (or the oneline, if no original merge commit was
    # . specified). Use -c <commit> to reword the commit message.
    #
    # These lines can be re-ordered; they are executed from top to bottom.
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    #
    # However, if you remove everything, the rebase will be aborted.
    #
    # Note that empty commits are commented out

We can replace the word pick by any of the given commands, but we are going to learn about how to remove or drop commit.

We will simply replace pick with d it will look something like this

d 734x3fe i want to remove this commit

Now save and exit the file

Psst.. press esc then type :wq to exit. There can be conflicts, solve them then continue rebase if there are commits remaining.

git rebase --continue

Congratulations 🥳, you’ve successfully removed the commit.

4. git cherry-pick

When all hell breaks loose and none of the options works then its time to use git cherry-pick

Using this command we can pick the commits we want and add them to a new branch and continue the work form that new branch.

git checkout -b <new branch name>

git cherry-pick <hash1-older>^..<hash2-newer>

And if you don’t want an inclusive cherry picking do

git cherry-pick <hash1-older>..<hash2-newer>

Also cherry picking a single commit can be done as

git cherry-pick <hash>

Voila! you’ve successfully removed the commit and the git history shows that nothing ever happened.

All done you’ve mastered the art of covering up your mistakes in git, Stay cool 😎