How to Remove Commit From History in Git

Abdul Jabbar Feb 02, 2024
  1. Remove Changes Locally in Git
  2. Remove Changes Forcefully in Git
How to Remove Commit From History in Git

In Git, adding confidential things to a repository is not a good idea, as it will ruin all our secrets and expose them to the whole world, which we don’t want. But sometimes, we intentionally or unintentionally add things we shouldn’t have.

Now we must sort out the matter and excise confidential information from our repo using Git commands. It’s not easy to remove it from Git history because Git has a more substantial background for keeping the repo’s history in various formats.

For this purpose, we should be much more careful in deep cleaning this blunder and remove the extra things from the branch of that repository.

Git has a wide range of tools for undoing commits, depending on the commit’s condition and size. Deleting can be accomplished in two different ways, depending upon our blunder and whether we have pushed the changes or not to the remote repository.

In this tutorial, we will learn about two different methods of removing commits locally and forcefully using Git commands.

Remove Changes Locally in Git

Remove Latest Commits

We can delete the latest commits with the help of the command git reset. This command is well known for undoing changes.

However, we can delete the most recent one through the following Git command.

git reset --hard HEAD~1

HEAD~1 specifies one commit before the HEAD. We will use the flag HEAD~N for deleting the specified commit with the command git reset.

git reset --hard HEAD~N

Another method could accomplish this by mentioning the exact commit hash id. The following command is the way to do it.

git reset --hard <sha1-commit-hash>

Undo the Middle Commit

With the help of the command git revert, we can insert a new commit which will undo the changes made by the specific middle commit. It is accomplished through the following Git command.

git revert <sha1-commit-hash>

Here, the main point is that git revert does not delete the specific middle commit. To delete it entirely from the history, we have to run git rebase along with the interactive argument with it, which is as follows:

git rebase -i <sha1-commit-hash>

Recover Deleted Commit

After deleting the commit, we can recover it using the following command if we want it back again.

git reflog

Remove Changes Forcefully in Git

If our teammates or we already push the changes to the remote repository, then Git has a smooth way to control this situation by running the command git push along with the flag --force.

This will delete the commit from the default remote repo that is the origin and will be available on the branch for future use.

git push origin HEAD --force

Note:

This method is not safe and is very critical in terms of usage; it may mess up our coworker’s local repositories. If someone from our team pulls this branch, it will merge into their work, and we will get this branch pushed back again.

So it is better and more convenient to start with a new branch in the same repository.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Remove