A dangling commit in Git is a commit object that exists in the repository but is not referenced by any branch, tag, or other reference. In other words, it is a commit that is not reachable from any named pointer in your repository’s history.

Dangling commits can be created in several ways, such as:

  • Deleting a branch without merging its changes.
  • Force-pushing changes that overwrite existing commits.
  • Amending a commit (e.g., using git commit --amend), which creates a new commit and leaves the old one unreferenced.
  • Rebasing or other history-rewriting operations that remove references to old commits.

Although these commits are not part of any active branch or tag, they still exist in the repository until Git’s garbage collection process eventually deletes them. If you know the commit’s SHA-1 hash, you can still access or recover it using commands like git checkout <SHA1> or by creating a new branch from it.

You can list dangling commits using:

git fsck --lost-found

This will show all unreachable commits and objects in your repository.