Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove commits that have been pushed from commit history

[For completeness sake: I am in a setting where I am accessing, from a single local copy, a private repo, of which I have sole access. There is no concern related to other users or even cloned versions of the repo in this particular setting.]

I have been trying to figure out how to completely delete a range of commits from a directory.

The important thing is that I am not functionally trying to undo the commit (i.e., I want to revert the repo to the state without the commit, using perhaps a reversing commit), I am trying to concretely remove the commit (i.e., I don't want it appearing in the commit history anymore). I have found a lot of answers that either explain how to do this locally, or how to functionally revert remotely — but not how to remove the commit from history completely.

This is because for the purposes of debugging continuous integrations, I made 40 commits, that I would like to squash.

How can do this? Again, the commits have all been pushed to a repo, and I want to remove these commits from the history. (It would be nice if I can merge all the changes into a single commit, but my main focus is removing the commit history so I can reintroduce the changes by hand if necessary.)

Here is an example repository, say I want to remove or squash the top 10 commits: https://github.com/jlumbroso/push-generated-file/commits/main

Thank you!

like image 423
Jérémie Avatar asked Aug 31 '25 20:08

Jérémie


1 Answers

You can do this by squashing the commits with git rebase -i BASE_COMMIT. That will open an editor, where you can leave the first commit at pick and change the rest to squash, then save and exit. You will be prompted to edit the commit message for that final commit.

This will result in a single commit with all of the changes that were in those 40 commits.

You'll then need to do a force push (e.g. git push -f origin branch) to the remote server to push the rebased branch. Note that GitHub does not run git gc by default, so anyone who knows the original commit ID of one of the commits can still view it, but it will not be a part of any branch.

like image 169
bk2204 Avatar answered Sep 04 '25 09:09

bk2204