美文网首页
丢弃的 commit 去哪了

丢弃的 commit 去哪了

作者: CondorHero | 来源:发表于2022-03-24 14:01 被阅读0次
丢弃的 commit 去哪了

前言

在 Git 中只要提交记录产生,无论你是 resetrebase 还是 commit --amend 等操作都没事,最终都能在 git reflog 中找到。那么问题来了,reflog 为什么能够找到?

HEAD 指针

首先,要明白提交的 commit 纪录一旦产生就一直都在,无论是 reset 还是 rebase 等操作并没有对 commit 纪录进程删除和更改,只是修改了指向 commit 的 HEAD 指针而已。

是否真的丢弃了 commit

当我们通过 git commit --amendgit rebase 创建一个新的提交或者通过 git reset --hard HEAD^ 撤销 commit 时,旧的提交仍然保留着呢。您通过 git log 看不到它们的原因是因为没有指向它们的指针,这些 commit 就是 dangling commit(悬挂的 commit)。

dangling commit

你可以通过 git fsck --lost-found 命令查看 dangling commit 列表。

例如,我们通过 git reset --hard HEAD^ 撤销 commit 产生了 dangling commit,然后通过 git fsck --lost-found 命令查看 dangling commit 列表:

`git fsck --lost-found` 命令查看 dangling commit 列表

这些 dangling commit 平时放着没事,如果太多当你做一些 git 操作的时候,比如大量的 dangling commit 导致 git rebase 出现如下提示:


Auto packing the repository in background for optimum performance. See "git help gc" for manual housekeeping. error: The last gc run reported the following. Please corrent the root cause and remove .git/gc.log. Automatic cleanup will not be performed until the file is removed. warning: There are too many unreachable loose objects; run 'git prune' to remove them.

这时你就得手动删除了,也很简单:

git reflog expire --expire=now --all
git gc --prune=now

相关文章

  • 丢弃的 commit 去哪了

    前言 在 Git 中只要提交记录产生,无论你是 reset 、 rebase 还是 commit --amend ...

  • git

    内容修改和回退 丢弃文件修改,还未commit 查看commit历史,可以看到commit_id 回退到某个提交 ...

  • Git 撤销Commit

    撤销未push到远端的commit,但保留该commit的修改 撤销未push到远端的commit,并且丢弃该co...

  • git reset 指令

    reset:重置;清零。 git reset 使用场景为丢弃commit后的commit信息、index信息或者源...

  • 左手去哪了?手机去哪了?

    左手去哪了?手机去哪了? 音符写梦 2014-3-10 16:16 每天14:53, 我都会左手拿上锁和钥匙, 右...

  • 时间去哪了,心去哪了。

    很多时候,我们想的,并不是时间去哪了,而是心去哪了。 一年365天,一天24小时,一小时60分钟,一分钟60秒,这...

  • 去哪了

    18.5万,能想起来的有9.5万,不知道那九万去哪了(*゚゚)

  • 去哪了

    戳破天空的 一定是那声呐喊 穿过苍穹 向前飞 也许在前方某个地方 会看到 失踪的从前 多想停下来 听一听风中的歌 ...

  • 去哪了

    渭城的风不再吹在我的脸上 不知记忆最深处那片竹林是否依然完好 奔跑的背影 追逐的男孩 你们都去哪儿了 冷落的球场 ...

  • 去哪了?

    我在过去的信里 寻找回忆 和你的那场相识 耗尽我所有的运气 隐隐作痛的回忆 不能这样烟消云散 一定还有别的原因 锁...

网友评论

      本文标题:丢弃的 commit 去哪了

      本文链接:https://www.haomeiwen.com/subject/tvgkjrtx.html