0

We frequently use rebase, amend, and other operations on our git repository that have created a lot of "unused" or intermediate commits.

More formally, unused commits are those that meet all of these criteria:

  • They do not have any child commits AND
  • They are not the HEAD of any branch (local or remote)
  • They are not the HEAD of any tag (local or remote)

Is there an easy way to (1) identify this list of commits and (2) permanently remove them?

Thanks!

user2490003
  • 10,706
  • 17
  • 79
  • 155
  • 2
    Are you in a hurry to drop them? Normally those objects get garbage collected after a while because even if there are no branches/tags pointing to them, there are other pointers still using them.. for a while (like reflog references). Anyway, you can use `git gc --aggresive --prune=now` but take a look at `git help gc` or https://git-scm.com/docs/git-gc – eftshift0 Nov 29 '19 at 17:33

1 Answers1

0
git fsck --unreachable

will show you unreachable commits that will be removed when running git prune. You can invoke

git prune

manually to remove all dangling objects.

Normally git garbage collection will automatically take care of that for you after a while. According to this answer, git gc runs:

  • git-prune
  • git-reflog
  • git-repack
  • git-rerere

Probably git prune does most of the cleaning that you need. You can also invoke git gc manually with more strict parameters, something like:

git gc --aggresive --prune=now

But make sure to read the documentation so as to tweak this command to fit your exact use.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136