How to Delete a Pushed Commit

How to Delete a Pushed Commit

Daily short news for you
  • How terrifying, Codeium - known as a competitor to Github Copilot, as it allows users to use it for free without limits. Recently, they introduced the Windsurf Editor - no longer just a VSCode Extension but a full Editor now - directly competing with Cursor. And the main point is that it... is completely free 🫣.

    » Read more
  • There is a rather interesting study that I came across: "Users never bother to read things they don't want to." (That's a bold statement, but it's more true than not. 😅)

    Don't believe it? I bet you've encountered situations where you've clicked on a button repeatedly and it doesn't respond, but in reality, it has displayed an error message somewhere. Or you've filled out everything and then when you hit the submit button, it doesn't go through. Frustrated, you scroll up or down to read and find out... oh, it turns out there's an extra step or two you need to take, right?

    It’s not far from the blog here. I thought that anyone who cares about the blog would click on the "Allow notifications" button just below the post. But the truth is, no one bothers to click it. Is it because they don't want to receive notifications? Probably not! I think it's because they just didn’t read that line.

    The evidence is that only when a notification pops up and takes up half the screen, or suddenly appears to grab attention, do they actually read it—and of course, it attracts a few more subscribers—something that was never achieved before.

    » Read more
  • A short article about the concepts of Functors, Applicatives, and Monads in functional programming that every time I read, I understand a little more 😅

    Functors, Applicatives, And Monads In Pictures

    » Read more

The Problem

A common issue that many people search for is "how to delete a pushed commit." I came across a blog post titled I accidentally committed the wrong changes, how do I fix it immediately? that ranks high on Google search results for this query. However, it only provides instructions on how to delete an "unpushed" commit.

To avoid confusion and provide readers with a more accurate solution to this problem, let's explore how to delete a pushed commit in this article.

Understanding the Essence of "Deletion"

I've heard many people say that Git is "easy" or confidently showcase their Git skills. Honestly, for me, Git is something quite challenging, and I can only use it at an intermediate level. This means initializing, cloning, pulling, pushing, branching, and some common daily Git commands. Git actually offers many more commands, not to mention Git hooks and advanced Git commands that I have never touched and am hesitant to explore because they can be quite complex.

Every individual, team, or organization has its own Git workflow, contributing to the diversity in Git usage.

Now, back to the problem at hand, let's temporarily set aside the complexities mentioned above. In essence, once you have pushed a commit to a remote repository, the likelihood of deleting that commit is quite risky. This depends on the purpose of deleting the commit. Is it a meaningless commit? Do you want to delete it to tidy up the Git tree due to a mistake in the commit? Simple thinking is that Git was created for team collaboration processes, where each commit pushed, someone else pulls, adds more code, and pushes again. Suppose you can delete any commit as you wish; wouldn't it lead to code chaos and disruptions?

At this point, you must give up hope of deleting any pushed commit because Git does not encourage that. However, if you want to delete the most recent commits, there is still a minimal risk possibility.

You may find a "golden time" to delete the most recent commits without affecting anyone, which is when you have pushed but no one has pulled yet. In this case, you can use the --force flag to push all previous commits from your local repository to the remote repository. Of course, the --force flag is something daunting and should not be abused. Most programs that provide the force option come with warnings not to use it unless you are absolutely sure about what you are doing. If you're uncertain, never use force. But before we delve into how to do that, there are two things to clarify.

  1. You want to force push on a shared branch (e.g., develop)

A shared branch typically refers to a branch where multiple people are contributing, such as develop, where team members continuously commit or merge code. Suppose the develop branch on the remote repository has the most recent commit that you want to delete. Fantastic! Take advantage of the time before anyone pulls or pushes anything new, and you can force push.

# Go back to the previous commit
$ git reset HEAD~1

# Force push all previous commits
$ git push --force

That's it! Your Git tree is now back to exactly how it was before the most recent commit, and that commit has completely disappeared.

  1. You want to force push on a branch you're working on alone (e.g., feature/xxx)

If you're working on a feature branch that hasn't been merged into develop, has a recent commit that needs to be deleted, and you want to remove it from Git, you can comfortably use force push to push the code again. Because you're the only one committing to this branch, the likelihood of someone pulling or pushing is low. The process is similar to the one mentioned above.

# Go back to the previous commit
$ git reset HEAD~1

# Force push all previous commits
$ git push --force

In Git, when you use --force to push, it replaces all previous local commits with the remote ones. So, commits made by others after your force push will be lost. Therefore, the golden rule here is to only force push on branches where you are the sole developer. For branches with multiple contributors, never use force. If you still decide to force push, gather everyone together and ask if they have committed anything new. If not, you can "request permission" to force push.

Moreover, many teams have workflows that involve using "protected branches," which means creating rules for each branch, specifying who can pull/push and who can merge. Suppose you have a commit merged into the develop branch, and it has restrictions on direct pushes. In that case, you clearly won't be able to force push develop back to a previous state. In such a case, it's likely that you'll need to discuss the issue with your project manager and request permission for a force push.

From the beginning until now, I've been mentioning force...force sounds heavy. So, besides that method, is there any way to delete a commit? To the best of my knowledge, there is no other way. If a commit is still in your local repository, you can follow the method in the blog post I accidentally committed the wrong changes, how do I fix it immediately? to delete it. However, once it's been pushed, the only way to delete it is through force push, which is like initializing the project from scratch with numerous existing commits, inevitably leading to conflicts or code loss.

Instead, consider "reverting" the commit. The revert command creates a new commit that undoes the changes made in a previous commit. Everything will return to the state before the commit you want to revert, but you won't be able to delete that commit.

$ git revert HEAD
$ git push

In conclusion, this article serves as a reference and does not encourage readers to apply these techniques to real projects. If you want to try, experiment on a test project first to gain experience before taking action.

Summary

Git was created to address team collaboration issues and more. Each commit in Git is like a commitment, and it's challenging to delete them once they're pushed to a remote repository. However, you can try to force push to delete the most recent commits or use a safer solution, such as reverting a commit.

Premium
Hello

5 profound lessons

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.
Author

Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.

Did you find this article helpful?
NoYes

Comments (0)

Leave a comment...