如何通过'git log'显示第一次提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5188914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:16:05  来源:igfitidea点击:

How to show first commit by 'git log'?

git

提问by Nyambaa

I have a project which has long history. I want to show the first commit on git.

我有一个历史悠久的项目。我想在 git 上显示第一次提交。

How do I do this?

我该怎么做呢?

回答by Nyambaa

I found that:

我找到:

git log --reverse

shows commits from start.

从一开始就显示提交。

回答by Chris Johnsen

Short answer

简答

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

(from tiho's comment. As Chris Johnsen notices, --max-parentswas introduced after this answer was posted.)

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

Explanation

解释

Technically, there may be more than one root commit. This happens when multiple previously independent histories are merged together. It is common when a project is integrated via a subtree merge.

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

The git.gitrepository has six root commits in its history graph (one each for Linus's initial commit, gitk, some initially separate tools, git-gui, gitweb, and git-p4). In this case, we know that e83c516is the one we are probably interested in. It is both the earliest commit and a root commit.

git.git资料库有其历史图6个提交(每一个Linus的初步承诺,gitk,最初一些独立的工具,混帐桂GitWeb里,和混帐-P4)。在这种情况下,我们知道这e83c516是我们可能感兴趣的。它既是最早的提交,也是根提交。

It is not so simple in the general case.

在一般情况下并不是那么简单。

Imagine that libfoohas 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.githas a date that precedes the commit with the earliest date in bar.git). At some point the developers of “bar” decide to incorporate libfoointo 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”.

想象一下,libfoo已经开发了一段时间,并将其历史记录保存在 Git 存储库 ( libfoo.git) 中。独立地,“bar”项目也在开发中(in bar.git),但开发时间不长libfoo(最早日期 inlibfoo.git的提交的日期早于最早日期 in 的提交bar.git)。在某些时候,“bar”的开发人员决定通过使用子树合并将libfoo合并到他们的项目中。在这次合并之前,确定“第一个”提交可能是微不足道的bar.git(可能只有一个根提交)。然而,在合并之后,有多个根提交,最早的根提交实际上来自libfoo的历史,不是“酒吧”。

You can find all the root commits of the history DAG like this:

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

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

For the record, if --max-parentsweren't available, this does also work:

作为记录,如果--max-parents不可用,这也有效:

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

If you have useful tags in place, then git name-revmight give you a quick overview of the history:

如果你有有用的标签,那么git name-rev可能会给你一个历史的快速概览:

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

Bonus

奖金

Use this often? Hard to remember? Add a git alias for quick access

经常用这个?很难记住?添加 git 别名以便快速访问

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

Now you can simply do

现在你可以简单地做

git first

回答by Mohamed Mansour

You can just reverse your log and just head it for the first result.

您可以反转您的日志,然后将其指向第一个结果。

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

回答by Matthew Flaschen

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

回答by MHC

Not the most beautiful way of doing it I guess:

我猜这不是最美丽的方式:

git log --pretty=oneline | wc -l

This gives you a number then

这会给你一个数字然后

git log HEAD~<The number minus one>

回答by TankorSmash

git log --format="%h" | tail -1gives you the commit hash (ie 0dd89fb), which you can feed into other commands, by doing something like

git log --format="%h" | tail -10dd89fb通过执行类似的操作,为您提供提交哈希(即),您可以将其输入其他命令

git diff `git log --format="%h" --after="1 day"| tail -1`..HEADto view all the commits in the last day.

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