我有一个Git项目,它有很长的历史。我想要显示第一个提交。

我怎么做呢?


当前回答

Git log——format="%h" | tail -1提供提交哈希值(即0dd89fb),您可以通过执行以下操作将其输入到其他命令

Git diff ' Git log——format="%h"——after="1 day"| tail -1 ' ..HEAD查看最后一天的所有提交。

其他回答

简短的回答

git rev-list --max-parents=0 HEAD

(来自tiho的评论。正如Chris Johnsen注意到的,——max-parents是在这个答案发布后引入的。)

解释

从技术上讲,可能有多个根提交。当多个先前独立的历史合并在一起时,就会发生这种情况。当一个项目通过子树合并进行集成时,这是很常见的。

git。git存储库的历史图中有六个根提交(每个根提交分别代表Linus的初始提交、gitk、一些最初单独的工具、git-gui、gitweb和git-p4)。在本例中,我们知道e83c516可能是我们感兴趣的。它既是最早的提交,也是根提交。

在一般情况下,事情就不那么简单了。

Imagine that libfoo has been in development for a while and keeps its history in a Git repository (libfoo.git). Independently, the “bar” project has also been under development (in bar.git), but not for as long libfoo (the commit with the earliest date in libfoo.git has a date that precedes the commit with the earliest date in bar.git). At some point the developers of “bar” decide to incorporate libfoo into their project by using a subtree merge. Prior to this merge it might have been trivial to determine the “first” commit in bar.git (there was probably only one root commit). After the merge, however, there are multiple root commits and the earliest root commit actually comes from the history of libfoo, not “bar”.

你可以像这样找到历史DAG的所有根提交:

git rev-list --max-parents=0 HEAD

为了记录,如果-max-parents不可用,这个也可以:

git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$"

如果你在适当的地方有有用的标签,那么git name-rev可能会给你一个历史的快速概述:

git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$" | git name-rev --stdin

奖金

经常使用这个吗?很难记住?为快速访问添加一个git别名

git config --global alias.first "rev-list --max-parents=0 HEAD"

现在你可以简单地做

git first

你可以把你的log颠倒过来,直接指向第一个结果。

git log --pretty=oneline --reverse | head -1

Git log——format="%h" | tail -1提供提交哈希值(即0dd89fb),您可以通过执行以下操作将其输入到其他命令

Git diff ' Git log——format="%h"——after="1 day"| tail -1 ' ..HEAD查看最后一天的所有提交。

要查看第一次提交的提交散列:

git rev-list --max-parents=0 HEAD 

要查看完整的git日志,包括第一次提交的commit消息:

git log $(git rev-list --max-parents=0 HEAD)

要查看所有git日志消息的倒序,从顶部的第一次提交(而不是底部)到底部的最后一次(最近)提交(而不是顶部):

git log --reverse

引用:

我是如何学习上面的第一个命令的:[已接受的答案]如何通过'git log'显示第一次提交?(上面的第二个命令是我自己贡献的) 我从@Nyambaa获得最多赞的回答中学到了git log

git log $(git log --pretty=format:%H|tail -1)