How to Delete a Commit in Git

Deleting a commit in Git is something you may do more often than you’d expect. With such a common task, you likely would find it handy to have a quick guide.

How to Delete a Commit in Git

This article answers how to delete a Git commit and other possible solutions you can try. So, if you make a mistake in your last commit or want to clean up your repo’s history, read on to find out.

Deleting a Commit in Git

You can delete a commit in Git in a few ways. If you don’t want to or can’t change the Git commit message, then the most common solution is to use “git reset” and “git revert” commands.

To use “git reset,” do this:

  1. Find the commit you want to delete with git log.
    Running the "git log" command
  2. Use git reset –soft [commit hash] to undo a commit command without “nuking” anything.
    Using the "git reset" command to reset a commit
  3. Or, use git reset –hard [commit hash] to reset to the commit before the one you want to delete.
  4. Replace “[commit hash]” with the hash of the commit you’re targeting.

Alternatively, you can also use “git rebase -i HEAD~[Number]” to rebase the last number of commits. Replace [number] with the number of commits. Git will show you a file that you can edit and remove the commit you wish to be gone. Only do this if you haven’t already pushed a commit.

This is a simple method on the surface, but be careful. It permanently removes all changes in the commits after the one you reset it to, so be sure this is exactly what you want.

Alternatively, you can use “git revert” like this:

  1. If you want to keep the changes in your working directory, use git revert [commit hash]. This command creates a new commit that undoes the changes from the commit you specify.
  2. After reverting, push the changes to the remote repository with git push.

Here is another alternative, in case you want to delete multiple, specific commits:

  1. Use git checkout [last commit hash] to check the last commit that should stay.
  2. Make a new branch with git checkout -b repair.
  3. Run git cherry-pick [specific commit hash] on the first commit you want to preserve.
  4. Repeat for the other commits you want to preserve.
  5. Run git checkout master to checkout master.
  6. Reset master to the last usable commit with git reset –hard [last commit hash].
  7. Merge the new branch to master with git merge repair.
  8. Then run git push -f origin master to push to the remote.

When Should You Delete Commits

There are a few situations when removing a commit is the best thing to do. Here are some of them.

Bad Code

One of the most common reasons why you’d want to get rid of a commit is because the code in it simply isn’t cutting it. Perhaps you have made a mistake and want to start over, or maybe you have realized that the result doesn’t match what you’re trying to achieve. For example, you might have added a function that conflicts with another part of your code. Deleting this commit allows you to go back, fix the issue, and then re-commit the corrected version.

If you’ve just committed a chunk of code and then spotted a glaring error, such as a bug or a piece of code that shouldn’t have been there, but you don’t have the time to fix it – that’s a good reason to delete the commit. Instead of letting this unnecessary or flawed piece of code sit in your project’s history (and potentially cause confusion or even catastrophic errors in bug fixing), you can delete that commit. Doing so keeps your project clean.

Sensitive Data

Accidentally committing sensitive data happens more often than you might think. Unsurprisingly, doing so can be a considerable security risk, whether personal or corporate. Anything from passwords and API keys to personal information could sneakily end up in your commit. And once it’s out there in the project space, anyone can access it and potentially breach your or your team’s security.

These accidents are perhaps even better reasons to delete a commit than mere bad code. You can edit faulty code out and not worry about it staying in the project history, but this isn’t a risk you’d want to take with sensitive data. For example, if you accidentally commit a file containing API keys, removing this commit prevents this key from falling into the wrong hands.

Keeping History Clean

Another solid reason to delete a Git commit is to keep your project history clean and simple. As you work on the project, your commit history becomes ever more cluttered with minor updates, experiments, or changes that are no longer relevant. When you want to look through history for any reason, such as figuring out why you made a certain change or reverting to a stable state after an experiment, a cluttered history could become a problem.

By deleting unnecessary commits, you keep your project history tidy and easy for you and others on your team to follow. That’s even more useful before merging a branch so you can present a clear, concise, and relevant history to whoever reviews your code. For example, if you have several commits that were just minor tweaks to a feature, you might want to squash them into a single commit to keep your history neat.

Best Practices and Tips

Before you delete a commit, follow some best practices to keep your project functional and avoid unintended consequences.

Backup Your Work

Before you delete any commit in Git, it’s a good idea to back up your work, even if you think it might be worthless to keep. It may seem like extra work, but a backup is a safety net in case things don’t go as planned.

You can create a backup by making a new branch that mirrors your repository’s current state. For example, before deleting a commit that introduced a new feature, create a backup branch “feature-backup.” That will give you the freedom to experiment and delete commits without the fear of losing important work. So, if you realize later that you need something from the deleted commit, you haven’t lost it for good. If you decide you no longer need that backup, you can delete the Git branch.

Don’t Delete Public Commits

When working with public repositories, especially in open-source projects or team environments, you should be extra cautious about deleting commits. Once a commit is pushed to a public repository and others have pulled it, deleting that commit can confuse or distract everyone involved and disrupt work. It’s a form of pulling a rug out from under someone – not a pleasant experience, but rather inconsiderate. If you must delete a commit that’s already public, tell your team or project contributors beforehand so they know what’s going on. This way, everyone is aware and can adjust their work accordingly.

Use Revert for Public Repos

If you still want to undo changes in a public repository, instead of deleting a commit permanently, you can revert a merge in Git. Unlike git reset, which effectively rewrites history, “git revert” creates a new commit that undoes the changes made in a previous commit.

Therefore, using this command is much safer and more transparent, especially when working with others. You are openly communicating to your team members that you may have made a mistake and are giving back a commitment that fixes it. If you pushed a commit with a significant bug, “git revert” allows you to quickly negate the effects of that bug while keeping the project’s history intact. Doing so maintains the project history’s integrity and keeps everyone’s local repository consistent with the remote.

Double-Check Before Deleting

One of the biggest risks when deleting commits in Git is losing the time and effort you put into that commit. To prevent this, review all the changes in each commit you want to remove. Use “git log or git show [commit hash]” to inspect the commits. If you’re removing a commit that added several functions to your code, make sure your project doesn’t need these functions to work or that they’re replicated elsewhere.

Committing to It

There are many reasons to delete a commit on Git, like keeping your history tidy, undoing confusing mistakes, or keeping your sensitive information safe from prying eyes. Whatever the reason, remember that there are two ways to do it – “git reset” and “git revert,” each serving a different purpose, so pick the one that’s right for your project. You can also rename the Git branch for additional clarity.

Have you ever had to delete a commit in Git? What was your experience, and do you have any tips to share? Leave a comment below and keep the conversation going.

Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.