How to Delete a Pushed Commit

How to Delete a Pushed Commit

Daily short news for you
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » 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.

Comments (0)

Leave a comment...