You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git log是否仅展示关联分支/标签的提交?如何查看悬空提交?

Git Log Behavior & Viewing All Commits (Including Unreachable Ones)

Great question—this gets to a core part of how Git tracks commit history, so let's unpack it step by step.

First: Does git log only show commits attached to branches/tags?

Short answer: Yes, by default (and even with --all), git log only displays reachable commits.

Reachable commits are those that can be traced back through a chain of parent commits starting from a "reference"—think local branches, remote branches like origin/main, tags, HEAD, or even stashes. When you reset a branch to an older commit and make new commits, the old commit chain you abandoned loses all references pointing to it. Without any reference anchoring it, Git considers those commits unreachable, so they won't show up in git log --all (since --all just includes all existing branches/tags/remote refs, not unreachable commits).

How to view all commits, including dangling/unreachable ones?

If you want to see every commit ever made (before Git's garbage collector cleans them up), here are a few reliable methods:

1. Use git fsck to find unreachable commits

git fsck checks Git's object database for inconsistencies, and can list all unreachable objects—including commits. Run this command first to get the IDs of unreachable commits:

git fsck --unreachable

This will output lines like unreachable commit abc123.... To view the history of these commits, pipe the IDs into git log:

git log --graph --oneline $(git fsck --unreachable | grep commit | cut -d' ' -f3)

2. Use the reflog to recover "lost" commit history

Git's reflog tracks every change to HEAD, including branch resets, merges, and checkouts. Even if you abandoned a commit chain, the reflog will have a record of the old HEAD position.

  • First, view the reflog to find the entry for your old branch state:
    git reflog
    
  • Then, view the log for that specific reflog entry (replace @{1} with the entry index you need):
    git log --graph --oneline HEAD@{1}
    

The reflog keeps entries for 30 days by default, so this works well for recently abandoned commits.

3. Note about Git garbage collection

Keep in mind: Git automatically runs garbage collection (gc) periodically, which deletes unreachable objects that are at least 2 weeks old. If your dangling commits are older than that, they might already be gone. But for recent changes, the methods above will work.

内容的提问来源于stack exchange,提问作者samshers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:39:29