W3docs

How to Delete Commits from a Branch in Git

In this short tutorial, you will find out how to delete commits from a branch in Git. Just follow the steps below to undo the things you didn’t mean.

This is a useful guide that will help you to delete commits from a branch in Git, accurately. To delete commits in Git, follow the steps below.

Deleting the latest commits

Generally, the git reset command is used for deleting the latest commits in Git.

Deleting the most recent commit

To delete the most recent commit, run the command below:

delete commits from git branch

git reset --hard HEAD~1

Note that <kbd class="highlighted">HEAD~1</kbd> means one commit prior to the <kbd class="highlighted">HEAD</kbd>.

Info

Here, the <kbd class="highlighted">HEAD</kbd> is the latest commit of the branch.

Deleting multiple latest commits

To delete the N-th latest commits, you should use <kbd class="highlighted">HEAD~N</kbd> as the argument of <kbd class="highlighted">git reset</kbd>.

How to Delete Commits from a Branch in Git

git reset --hard HEAD~N

As an alternative to this method, you can select the appropriate commit by its hash instead of <kbd class="highlighted">HEAD~N</kbd>. Here is how to do it:

delete the commit id

git reset --hard &lt;sha1-commit-hash&gt;

Deleting a Commit in the Middle of the History

Undoing the Changes of the "Middle" Commit

There exists a safe way of deleting a commit in the middle of the history: using the git revert command. It adds a new commit, which undoes everything the middle commit has changed.

How to Delete Commits from a Branch in Git

git revert &lt;sha1-commit-hash&gt;

Deleting the "Middle" Commit from the History.

Take into account that <kbd class="highlighted">git revert</kbd> doesn't delete the middle commit. If you want to delete it from the history, then run git rebase in interactive mode:

How to Delete Commits from a Branch in Git

git rebase -i &lt;sha1-commit-hash&gt;

Then, an editor opens that shows up the commits following the one you have selected. It will offer you to input the command for each commit. All you need to do is typing "drop" at the beginning of each commit you want to delete.

Deleting a Commit in the Middle of the History
Danger

Be careful while using the git rebase command, as it may cause sudden problems. So, it is more recommended to use the git revert command.

Pushing changes

In case you have already pushed your commits, then you need to run git push with the <kbd class="highlighted">--force</kbd> flag to delete the commits from the remote (suppose, the name of remote is origin, which is by default):

force push to delete commit

git push origin HEAD --force
Danger

--force overwrites the remote branch on the basis of your local branch. It destroys all the pushed changes made by other developers. It refers to the changes that you don't have in your local branch.

Here is an alternative and safer way to push combined commits:

How to Delete Commits from a Branch in Git

git push --force-with-lease origin HEAD
Info

--force-with-lease is considered a safer option that will not overwrite the work done on the remote branch in case more commits were attached to it (for instance, by another developer). Moreover, it helps you to avoid overwriting another developer's work by force pushing.

Finding the deleted commit

Finally, in case you want to find the deleted commit, you can do it with the help of the git reflog command:

delete the commit from git branch

git reflog

How git reset Works

The <kbd class="highlighted">git reset</kbd> command is a powerful tool for undoing changes.

<kbd class="highlighted">Git reset</kbd> has some similarities with the git checkout command because both of the commands work on <kbd class="highlighted">HEAD</kbd>. <kbd class="highlighted">Git checkout</kbd> operates only on <kbd class="highlighted">HEAD</kbd> reference pointer, and git reset passes that <kbd class="highlighted">HEAD</kbd> reference pointer and present branch reference pointer.

How git reflog Works

This command is generally used for recording updates made to the branches. With the help of the <kbd class="highlighted">git reflog</kbd> command, you can return to the commits even to those that have not been referenced by any branch of tag. Reflog contains information about the preceding state of branches and allows returning to that state if needed.

How git rebase Works

The <kbd class="highlighted">git rebase</kbd> command is aimed at integrating changes from a branch to another. It is used as an alternative to the git merge command. However, there is a notable difference between these two commands: <kbd class="highlighted">git rebase</kbd> rewrites the commit history for creating a more linear project history. There are two modes of <kbd class="highlighted">git rebase</kbd> command: standard and interactive. In standard mode <kbd class="highlighted">git rebase</kbd> will automatically apply the commits in the current working branch to the passed branch’s head. The current branch will be rebased onto <base> .