Deleting specific git commits

I have a repo with a file named main and in my committing history I committed several changes to it,
now I want to delete it and every commit I have done to it, can I just delete every commit with the word ‘main’ in its name?

Most likely you’ll need to look at the file path rather than commit messages to do that, the examples for git filter-branch describe (among other things) how to remove a file from history. Alternatively, if the history isn’t too complex, you could use git rebase to manually edit the relevant commits.

Two words of caution though:

  1. Either method will change all commits starting from the first one that needs to be modified. So if your repository is shared with others, they’ll all notice the change and have to manually accept it (or not). In that case you should carefully consider if you really want to rewrite history.
  2. If your repository is on Github and you’re trying to remove sensitive data, after you’ve fixed the repository you should contact support and ask them to make sure there are no caches or other references to the sensitive data left. See also: Removing sensitive data from a repository

Thanks, this is very helpful

1 Like