How to Delete a Commit in Git

Before diving into how to delete commits in Git, it’s important to have a basic understanding of what commits are and how they work in Git.

Commits allow you to track changes over time and revert to previous versions if needed. They form the backbone of Git’s powerful version control capabilities.

When you make changes to your project, you stage them first and then commit them to the local repository. You can then push commits to remote repositories to share with others. Each commit gets a unique SHA-1 hash ID that identifies it.

Commits build on top of each other. By default, each commit is based on the previous one, known as the parent commit.

Now that you know what commits are, let’s look at why you may need to delete them at times.

How to Delete a Commit in Git

Deleting the problematic commit gives you a chance to fix issues, secure sensitive data, and curate a clean commit timeline.

However, deleting commits also carries a risk of losing work, so extra caution needs to be exercised. Thankfully, Git provides specialized commands to delete commits safely.

Use “git reset” Command

The git reset command allows undoing commits by resetting the current branch to a previous commit. It erases commits after the specified commit.

Here are the steps to delete a commit with git reset:

  1. Identify the faulty commit’s hash ID using git log.
  2. Decide which reset mode you want:
    • --soft – Resets to commit but keeps changed files staged.
    • --hard – Resets completely and deletes changes from the working tree.
  3. Run the reset command: git reset --[soft/hard] [commit-hash]
  4. Forced push changes to the remote repository with git push --force.

A --hard reset is more dangerous as it deletes uncommitted changes as well, so only use it if necessary.

The major downside of git reset is that it rewrites project history, which can cause issues for any distributed branches.

Use “git revert”

If you want a safer method, git revert is a good alternative to git reset. Instead of deleting commits, git revert creates a new commit that reverses changes from a problematic commit.

Here is how to revert a faulty commit:

  1. Use git log to identify the commit.
  2. Run the command: git revert [commit-hash]
  3. It will open an editor to enter a revert commit message.
  4. Save the message and close the editor to complete the revert.
  5. Push changes to the remote repository.

The benefit of git revert is that it doesn’t rewrite project history. So it’s safer for public repositories and remote team members.

A limitation is that reverting merge commits can be tricky in some cases.

Use “git rebase -i HEAD~[Number]”

An interactive rebase can also allow deleting commits by rebasing a branch and editing the commit list to remove unwanted commits.

Here is how to do an interactive rebase:

  1. Determine how many recent commits you want to rebase with HEAD~X.
  2. Run git rebase -i HEAD~X.
  3. An editor will open with a list of commits.
  4. Delete the line of the commit you want to remove.
  5. Save and close the file to perform rebase.

But it also rewrites history, so should only be done on local commits. The major risk is losing commits accidentally, so make sure your work is backed up first.

Related Posts
How to Use WhatsApp on iPad
How to Use WhatsApp on iPad

WhatsApp is a popular messaging app that can be used Read more

How to transfer laptop to laptop data?
share files

Today we have come up with the answers to the Read more

How to Share Instagram Reels on Facebook Automatically
share instagram reels on facebook automatically

Want to automatically share your Instagram Reels to your Facebook Read more

How to Decrease Your Snapchat Score
hide score on snapchat

Are you concerned about your Snapchat score and want to Read more

Ali Rehman

Rehman is your go-to source for all things social media. With a finger on the pulse of the latest trends and thriving online communities, his articles provide an engaging insight into the dynamic world of social platforms.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments