How to delete non-relevant commits

Question regarding commits that float around after an --amend:

  1. Push a commit with a reference to an issue:

    git init
    git add foo.py
    git add README.md
    git commit -m “Fixes a thing. Issue reference: #164
    git push origin my-cool-pr

  2. Realizing you forgot something, you --amend:

    git add bar.py
    git commit -a --amend
    git push origin my-cool-pr -f

3. It’s not your day, you made a spelling mistake in README.md

vim README.md
git add README.md
git commit -a --amend
git push origin my-cool-pr -f

Now visit issue #164. It will have 3 ‘back-references’ to those individual commits. However, only the last one is relevant, since you’ve been amending. 

How do I delete those commits that are still floating around?

There isn’t a way for you to manually delete those commits. You would have to contact private support at https://github.com/contact to have them removed.

For this reason, I typically don’t recommend adding issue references in commit messages themselves. Rather, I recommend adding the issue references to the PR body when a series of commits addresses or fixes an issue.

I hope that helps!

1 Like