paint-brush
How to Delete Commits From Remote in Gitby@timmouskhelichvili
364,422 reads
364,422 reads

How to Delete Commits From Remote in Git

by Tim MouskhelichviliMay 31st, 2022
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

To delete commits from remote, you can use the git reset command if your commits are consecutive from the top or an interactive rebase otherwise. After you delete the commits locally, push those changes to the remote using the git push command with the force option.
featured image - How to Delete Commits From Remote in Git
Tim Mouskhelichvili HackerNoon profile picture

Manipulating history is very common for developers who work with Git regularly. Indeed, developers often need to remove commits from the Git history. Luckily, Git provides many commands to make this operation possible.


Let’s get to it 😎.

Step 0 - Preparation

Before manipulating the Git history, ensure that your working directory is clean of any changes using the git status command.

Step 1 - Delete commits locally

To delete commits from a remote server, first, you will need to remove them from your local history.

1.1 For consecutive commits from the top

If the commits you want to remove are placed at the top of your commit history, use the git reset --hard command with the HEAD object and the number of commits you want to remove.


git reset --hard HEAD~1

This command will remove the latest commit.


git reset --hard HEAD~3

This command will remove the latest three commits.


You can also remove up to a specific commit using a commit’s hash, like so:

git reset --hard <hash>

1.2 For non-consecutive commits

If, however you want to remove non-consecutive commits, you’ll need to use an interactive rebase.


  • [ ]Find the last commit hash containing all of the commits you want to remove using the git reflog command.
  • [ ]Start an interactive rebase with git rebase -i <hash>.
  • [ ]In the edit screen, find the commit lines you want to remove and remove them.
  • [ ]Save and exit (you may need to resolve the conflicts)
  • [ ]End the interactive rebase with git rebase --continue or start over by aborting the rebase.

Step 2 - Delete the commits from remote

To delete commits from remote, you will need to push your local changes to the remote using the git push command.


git push origin HEAD --force


Since your local history diverges from the remote history, you need to use the force option.

Final thoughts

As you can see, Git makes it easy to delete commits from a remote server.


However, you need to be careful when using the git push command with the force option because you could lose progress if you are not cautious.


Thank you for reading!