Skip to content

Instantly share code, notes, and snippets.

@tigerhawkvok
Last active February 27, 2026 00:30
Show Gist options
  • Select an option

  • Save tigerhawkvok/0123f344a976447db1f3bfd342a134ca to your computer and use it in GitHub Desktop.

Select an option

Save tigerhawkvok/0123f344a976447db1f3bfd342a134ca to your computer and use it in GitHub Desktop.
Find dangling commits
#!python3
import json
import subprocess
from pathlib import Path
filterPhrases = []
danglingSTDOut = subprocess.run(["git", "fsck", "--lost-found"], stdout= subprocess.PIPE, stderr= subprocess.PIPE).stdout.decode("utf-8")
dangling = [x.split(" ").pop() for x in danglingSTDOut.splitlines() if "commit" in x]
details = [subprocess.run(["git", "show", "-s", "--format=reference", x], stdout= subprocess.PIPE, stderr= subprocess.PIPE).stdout.decode("utf-8") for x in dangling]
filtered = [x.strip() for x in details if any([len(filterPhrases) == 0, *[_phrase not in x for _phrase in filterPhrases]])]
sortCommits = sorted(filtered, key=lambda x: x.rsplit(", ", 1)[-1], reverse= True)
Path("dangling.json").write_text(json.dumps(sortCommits, indent= 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment