声明:这个问题纯粹是信息性的,不代表我正在经历实际问题。我只是试图为了解决问题而进行尝试(因为我喜欢解决问题,我知道你也是一样)。
所以我在使用Git时,试图使修改后的提交过期。我的引用日志看起来像这样:
4eea1cd HEAD@{0}: commit (amend): amend commit
ff576c1 HEAD@{1}: commit: test: bar
5a1e68a HEAD@{2}: commit: test: foo
da8534a HEAD@{3}: commit (initial): initial commit
这意味着我做了两个提交(da8534a
和 5a1e68a
),然后进行了第三个提交ff576c1
,并使用4eea1cd
进行了修改。
正如所预期的那样,我的git log
大致如下:
* 4eea1cd (HEAD, master) amend commit
* 5a1e68a test: foo
* da8534a initial commit
根据我所知道的提交到期处理,某一天(默认情况下最可能在30天内),git gc
应该会清除ff576c1
. 现在我不想等30天才能看到它被清除,因此我开始运行一些命令,首先是:
git fsck --unreachable --no-reflogs
正如预期的一样,这又给了我:
unreachable blob 5716ca5987cbf97d6bb54920bea6adde242d87e6
unreachable tree 1e60e555e3500075d00085e4c1720030e077b6c8
unreachable commit ff576c1b4b6df57ba1c20afabd718c93dacf2fc6
我非常有把握要废弃这个可怜而孤独的ff576c1
提交,然后我运行了git reflog expire
:
git reflog expire --dry-run --expire-unreachable=now --all
当时给了我:
would prune commit: test: bar
would prune commit (amend): amend commit
起初我以为我的HEAD
没有引用master
,但是正如你可以在我之前给出的git log
输出中看到的那样,它实际上是引用了的。另外,cat .git/HEAD
也证实了这一点(产生ref: refs/heads/master
)。无论如何,即使那个想法很愚蠢,因为4eea1cd
是我的master
分支的最新提交。所以,我很困惑这两个命令为什么不能给出相同的提交,并想知道为什么
4eea1cd
可能无法到达,因为它是我的master
分支的实际最新提交。你有任何想法吗?
编辑:我刚刚注意到,如果我将
--rewrite
选项添加到git reflog expire
中,就像这样:git reflog expire --dry-run --expire-unreachable=now --all --rewrite
那么我只得到了修改后的提交:
would prune commit: test: bar
我还是不明白,因为根据 git help reflog
:
--rewrite
While expiring or deleting, adjust each reflog entry to ensure that
the old sha1 field points to the new sha1 field of the previous
entry.
在我的情况下这是没有意义的,至少我不明白,因为显然它确实改变了一些东西。