git 如何引用初始提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1006775/
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 06:37:07  来源:igfitidea点击:

How to reference the initial commit?

gitgit-commit

提问by Bryan Larsen

I've got a script that needs to reference the initial commit in a repository. git has the special reference HEAD, but doesn't have the corresponding TAIL. I cannot find anything in git help rev-parsethat would seem to help me.

我有一个脚本需要引用存储库中的初始提交。git 有特殊的引用HEAD,但没有相应的TAIL. 我找不到任何git help rev-parse似乎对我有帮助的东西。

Here's what I'd like to do:

这是我想要做的:

git show TAIL

Here's one option I have:

这是我的一种选择:

git show `git log --reverse | if read a commit ; then echo $commit ; fi`

That's pretty hacky and depends on the output of git log not changing.

这非常棘手,取决于 git log 的输出不会改变。

Right now I just tag the initial commit and use that as my refspec. However, I'd like to release a general tool, so that's not a great option.

现在我只是标记初始提交并将其用作我的参考规范。但是,我想发布一个通用工具,所以这不是一个很好的选择。

回答by Jakub Nar?bski

Do not use git-log for scripting: use either git-rev-list, or git-logwith specified custom format (--format=*<sth>*option).

不要使用 git-log 编写脚本:使用git-rev-list, 或git-log指定的自定义格式(--format=*<sth>*选项)。

There is additional problem with your question: there can exist more than onesuch TAIL root commit (parentless commit) in a repository (even if we discount disconnected branches, such as 'html', 'man' and 'todo' in git.git repository). This is usually result of joining separate projects in one, or using subtree merge of separately developed subproject.

您的问题还有其他问题:存储库中可能存在多个这样的 TAIL 根提交(无父提交)(即使我们不考虑断开连接的分支,例如 git.git 中的“html”、“man”和“todo”)存储库)。这通常是将单独的项目合二为一,或使用单独开发的子项目的子树合并的结果。

For example git repository has 6 root commits: git-gui, gitk (subtree-merged), gitweb (merged in, no longer developed separately), git mail tools (merged very early in project history), and p4-fast-export (perhaps accidental). That is not counting roots of 'html and 'man' branches, "convenience" branches which contains pre-generated documentation, and 'todo' branch with TODO list and scripts.

例如 git 存储库有 6 个根提交:git-gui、gitk(子树合并)、gitweb(合并进来,不再单独开发)、git 邮件工具(在项目历史的早期合并)和 p4-fast-export(可能是偶然的)。这不包括 'html 和 'man' 分支的根,包含预先生成的文档的“convenience”分支,以及带有 TODO 列表和脚本的 'todo' 分支。



If you have git 1.7.4.2 or newer, you can use the --max-parentsoption:

如果您有 git 1.7.4.2 或更高版本,则可以使用以下--max-parents选项:

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

Otherwise, you can get list of all parentless (root) commits accessible from current branch using:

否则,您可以使用以下命令获取从当前分支可访问的所有无父(根)提交的列表:

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

回答by Robert Munteanu

git rev-list HEAD | tail -n 1is a more stable option.

git rev-list HEAD | tail -n 1是更稳定的选择。