获取所有 Git 提交的列表,包括“丢失”的提交那些

发布于 2024-10-14 00:55:16 字数 289 浏览 7 评论 0原文

假设我有一个像这样的图表:

A---B---C---D (master)
     \
      \-E---F (HEAD)

如果我执行 git log --all --oneline ,我将获得所有六个提交。

但如果图表是:

A---B---C---D (master, HEAD)
     \
      \-E---F

我将看不到 E 和 F。我可以让 Git 告诉我所有提交,包括那些未命名的分支上的提交吗?

Let's say that I have a graph like this:

A---B---C---D (master)
     \
      \-E---F (HEAD)

If I do git log --all --oneline, I will get all six of my commits.

But if the graph is:

A---B---C---D (master, HEAD)
     \
      \-E---F

I will not see E and F. Can I get Git to tell me all the commits, including those on branches which are not named?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

鲜肉鲜肉永远不皱 2024-10-21 00:55:16

尝试:

git log --reflog

通过假装 reflogs (git reflog) 提到的所有对象都在命令行上列为 来列出所有 git 提交。

Try:

git log --reflog

which lists all git commits by pretending that all objects mentioned by reflogs (git reflog) are listed on the command line as <commit>.

勿忘初心 2024-10-21 00:55:16

拯救我生命的是下面的命令:

git reflog

你会发现一个屏幕,其中有对 git 的历史提交,如下所示:

< img src="https://i.sstatic.net/Bkh4L.png" alt="在此处输入图片描述">

此时,只需找到 HEAD@{X}< /code> 您需要的,创建一个临时分支并像这样移动到它:

git checkout -b temp_branch HEAD@{X}

这样您将拥有一个包含丢失提交的临时分支,而无需重新设置或破坏更多 git 存储库。

希望这有帮助...

What saved my life was the following command:

git reflog

There you find a screen with history commits done to git like this one:

enter image description here

At this point, you only have to find the HEAD@{X} that you need, create a temporary branch and move to it like this:

git checkout -b temp_branch HEAD@{X}

That way you will have a temporary branch with your lost commit without rebasing or breaking even more your git repository.

Hope this helps...

北方的巷 2024-10-21 00:55:16

不是特别容易——如果你失去了指向树枝尖端的指针,那就就像大海捞针一样。您可以找到所有似乎不再被引用的提交 - git fsck --unreachable 将为您执行此操作 - 但这将包括您在 git 之后丢弃的提交commit --amend,您重新建立基础的分支上的旧提交等等。因此,立即查看所有这些提交很可能需要费力地浏览太多信息。

所以轻率的答案是,不要忘记你感兴趣的事情。更严重的是,默认情况下,引用日志将保存对你在过去 60 天左右使用过的所有提交的引用。更重要的是,他们将提供一些关于这些提交的背景信息。

Not particularly easily- if you've lost the pointer to the tip of a branch, it's rather like finding a needle in a haystack. You can find all the commits that don't appear to be referenced any more- git fsck --unreachable will do this for you- but that will include commits that you threw away after a git commit --amend, old commits on branches that you rebased etc etc. So seeing all these commits at once is quite likely far too much information to wade through.

So the flippant answer is, don't lose track of things you're interested in. More seriously, the reflogs will hold references to all the commits you've used for the last 60 days or so by default. More importantly, they will give some context about what those commits are.

攒一口袋星星 2024-10-21 00:55:16

当我解决这个问题时,我使用以下命令:

git reflog |  awk '{ print $1 }' | xargs gitk

这让我可以可视化最近已变成无头的提交。

我将其封装在名为 ~/bin/git-reflog-gitk 的脚本助手中。

编辑: - 我的 git-reflog-gitk 包装器是多余的 - 因为你现在可以直接调用 gitk --reflog

When I tackle this issue I use the following command:

git reflog |  awk '{ print $1 }' | xargs gitk

This lets me visualise recent commits which have become headless.

I have this wrapped up in a script helper called ~/bin/git-reflog-gitk.

Edit: - my git-reflog-gitk wrapper is redundant - as you can now call gitk --reflog directly

追风人 2024-10-21 00:55:16

就像 @Kieran 的答案,但对于控制台:
<代码>
git log --oneline --all --graph --decorate $(git reflog | awk '{print $1}')

Like @Kieran 's Answer, but for the console:

git log --oneline --all --graph --decorate $(git reflog | awk '{print $1}')

花期渐远 2024-10-21 00:55:16
git log --reflog

救了我!我在合并 HEAD 时丢失了我的,并且找不到我最近的提交!未显示在源代码树中,但 git log --reflog 显示了我之前的所有本地提交

git log --reflog

saved me! I lost mine while merging HEAD and could not find my lates commit! Not showing in source tree but git log --reflog show all my local commits before

孤檠 2024-10-21 00:55:16

我如何解决这个问题?使用 git fsck 和日志记录!

首先创建一个包含丢失(无法访问)的提交和 blob 的文件。 (注意:如果你做了像 git gc 这样的事情,那么它会垃圾收集所有提交的内容,并且你不会在这里找到它们!)

$git fsck --lost-found > lost_found.commits

这会给你一个像这样的文件:

悬空提交 dec2c5e72a81ef06963397a49c4b068540fc0dc3
悬空斑点 f8c2579e6cbfe022f08345fa7553feb08d60a975
悬空斑点 0eb3e86dc112332ceadf9bc826c49bd371acc194
悬空斑点 11cbd8eba79e01f4fd7f496b1750953146a09502
悬空提交 18733e44097d2c7a800650cea442febc5344f9b3
悬空斑点 1e53a5cdb3ecdde27081ec6e8b31e4070106ee05

然后,您可以使用您喜欢的文本编辑器打开此文件,从那里复制提交/博客哈希值。 (*咳嗽* vim 宏非常适合这个*咳嗽*)

现在您可以使用 git log --oneline
或者,gitk、tig 或任何其他 git 查看器也应该可以工作。

在您的情况下,如果您找到提交 F 的哈希值,日志将向您显示类似这样的内容,

A---B---E---F

快速而简单!现在您可以找到所有这些悬而未决的提交背后的上下文。

PS 是的,我知道,发帖晚了,但是哦,好吧,有人可能会在这里找到它并发现它有用。 (很可能是 6 个月后我再次用 google 搜索这个)

How I solve this problem? Use git fsck and logging!

First create a file containing lost (unreachable) commits and blobs. (NOTE: if you did something like git gc then it will garbage collect all of they commits and you won't find them here!)

$git fsck --lost-found > lost_found.commits

That gives you a file like this:

dangling commit dec2c5e72a81ef06963397a49c4b068540fc0dc3
dangling blob f8c2579e6cbfe022f08345fa7553feb08d60a975
dangling blob 0eb3e86dc112332ceadf9bc826c49bd371acc194
dangling blob 11cbd8eba79e01f4fd7f496b1750953146a09502
dangling commit 18733e44097d2c7a800650cea442febc5344f9b3
dangling blob 1e53a5cdb3ecdde27081ec6e8b31e4070106ee05

You can then open this file with you favorite text editor to copy the commit/blog hashes from there. (*cough* vim macros works great for this *cough*)

Now you can log back from this commit with something like git log --oneline <commit hash>.
Alternatively, gitk, tig, or any other git viewer should work.

In your case if you find the hash for commit F the log will show you something like this,

A---B---E---F

Quick and easy! Now you can find the context behind all of those dangling commits.

P.S. Yes, I know, late post, but oh well, somebody might find it here and find it useful. (Mostly likely me in 6 months when I google this again)

撩人痒 2024-10-21 00:55:16

我很幸运地通过查看位于 .git/logs/HEAD 的引用日志来恢复提交,

然后我不得不向下滚动到文件末尾 ,我找到了我刚刚丢失的提交。

I've had luck recovering the commit by looking at the reflog, which was located at .git/logs/HEAD

I then had to scoll down to the end of the file, and I found the commit I just lost.

紅太極 2024-10-21 00:55:16

实际上 git fsck 可以用来查找所有丢失的提交,你只需要正确的选项:

git fsck --unreachable --no-reflogs

--unreachable 单独是不够的,因为某些提交可能仍然被引用重新记录。如果您需要非常清晰地查看整个提交历史记录,您可以为类似的内容创建一个别名:

git log --all --decorate --oneline --graph $(git fsck --no-reflogs --unreachable | awk '{if ($2 == "commit") print $3}')

说实话,我不确定在最后一个命令中您是否需要 --unreachable 选项,鉴于 git log 默认情况下遍历祖先(除非指定 --no-walk)。我不会打赌,但我认为没有必要。

Actually git fsck can be used to find all the lost commits, you just need the right option:

git fsck --unreachable --no-reflogs

--unreachable alone is not enough because some commits might still be referenced by the reflog. If you need a pretty clear view of the entire commit history, you can create an alias for something like this:

git log --all --decorate --oneline --graph $(git fsck --no-reflogs --unreachable | awk '{if ($2 == "commit") print $3}')

To be honest I am not sure if in this last command you need the --unreachable option, given that git log traverse the ancestors by default (unless --no-walk is specified). I would not bet on it, but I think it is not necessary.

樱花落人离去 2024-10-21 00:55:16

我们有时无法通过 git log 获取所有提交详细信息,因此要查看此...

对于 Mac:进入您的 git 项目并输入:

$ nano .git/logs/HEAD

以查看其中的所有提交,或者:

$ gedit .git/logs/HEAD

查看其中的所有提交,

然后您可以在任何您喜欢的浏览器中进行编辑。

We'll git log sometimes is not good to get all commits detail, so to view this...

For Mac: Get into you git project and type:

$ nano .git/logs/HEAD

to view you all commits in that, or:

$ gedit .git/logs/HEAD

to view you all commits in that,

then you can edit in any of your favourite browser.

若能看破又如何 2024-10-21 00:55:16

@bsimmons

git fsck --lost-found | grep commit

然后为每个分支创建一个分支:

$ git fsck --lost-found | grep commit
Checking object directories: 100% (256/256), done.
dangling commit 2806a32af04d1bbd7803fb899071fcf247a2b9b0
dangling commit 6d0e49efd0c1a4b5bea1235c6286f0b64c4c8de1
dangling commit 91ca9b2482a96b20dc31d2af4818d69606a229d4

$ git branch  branch_2806a3 2806a3
$ git branch  branch_6d0e49 6d0e49
$ git branch  branch_91ca9b 91ca9b

现在许多工具将向您显示那些丢失的提交的图形可视化。

@bsimmons

git fsck --lost-found | grep commit

Then create a branch for each one:

$ git fsck --lost-found | grep commit
Checking object directories: 100% (256/256), done.
dangling commit 2806a32af04d1bbd7803fb899071fcf247a2b9b0
dangling commit 6d0e49efd0c1a4b5bea1235c6286f0b64c4c8de1
dangling commit 91ca9b2482a96b20dc31d2af4818d69606a229d4

$ git branch  branch_2806a3 2806a3
$ git branch  branch_6d0e49 6d0e49
$ git branch  branch_91ca9b 91ca9b

Now many tools will show you a graphical visualization of those lost commits.

最佳男配角 2024-10-21 00:55:16

如果您使用 Git 扩展 GUI,如果您选中“查看 -> 显示引用日志引用”,它可以向您显示悬空提交的图形可视化。
这将显示树中的悬空提交,就像所有其他引用的提交一样。这样就可以更轻松地找到您要查找的内容。

请参阅此图片进行演示。图像上的提交 C2、C3、C4 和 C5 是悬空的,但仍然可见。

If you use Git Extensions GUI it can show you a graphical visualization of dangling commits if you check "View -> Show reflog references".
This will show dangling commits in the tree, just like all other referenced ones. This way it is way easier to find what you are looking for.

See this image for demonstration. Commits C2, C3, C4, and C5 on the image are dangling but still visible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文