如何确定 Git 分支的创建时间?

发布于 2024-08-21 12:53:52 字数 78 浏览 8 评论 0原文

有没有办法确定 Git 分支的创建时间?

我的存储库中有一个分支,并且我不记得创建它,并且认为也许看到创建时间戳会唤起我的记忆。

Is there a way to determine when a Git branch was created?

I have a branch in my repository, and and I don't remember creating it and thought maybe seeing the creation timestamp would jog my memory.

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

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

发布评论

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

评论(16

┾廆蒐ゝ 2024-08-28 12:53:52

正如评论中指出的 和 Jackub 的回答,只要您的分支比配置设置中设置的天数新gc.reflogexpire(默认为 90 天),然后您可以利用 reflog 来查找分支引用首次创建的时间。

请注意, git reflog 可以占用大部分 git log 标志。还请注意,HEAD@{0} 样式选择器实际上是时间概念,实际上,它是作为日期字符串处理(以一种黑客方式)的。这意味着您可以使用标志 --date=local 并获得如下输出:

$ git reflog --date=local
763008c HEAD@{Fri Aug 20 10:09:18 2010}: pull : Fast-forward
f6cec0a HEAD@{Tue Aug 10 09:37:55 2010}: pull : Fast-forward
e9e70bc HEAD@{Thu Feb 4 02:51:10 2010}: pull : Fast forward
836f48c HEAD@{Thu Jan 21 14:08:14 2010}: checkout: moving from master to master
836f48c HEAD@{Thu Jan 21 14:08:10 2010}: pull : Fast forward
24bc734 HEAD@{Wed Jan 20 12:05:45 2010}: checkout: moving from 74fca6a42863ffacaf7ba6f1936a9f228950f657 
74fca6a HEAD@{Wed Jan 20 11:55:43 2010}: checkout: moving from master to v2.6.31
24bc734 HEAD@{Wed Jan 20 11:44:42 2010}: pull : Fast forward
964fe08 HEAD@{Mon Oct 26 15:29:29 2009}: checkout: moving from 4a6908a3a050aacc9c3a2f36b276b46c0629ad91 
4a6908a HEAD@{Mon Oct 26 14:52:12 2009}: checkout: moving from master to v2.6.28

有时使用 --date=relative 也可能很有用:

$ git reflog --date=relative
763008c HEAD@{4 weeks ago}: pull : Fast-forward
f6cec0a HEAD@{6 weeks ago}: pull : Fast-forward
e9e70bc HEAD@{8 months ago}: pull : Fast forward
836f48c HEAD@{8 months ago}: checkout: moving from master to master
836f48c HEAD@{8 months ago}: pull : Fast forward
24bc734 HEAD@{8 months ago}: checkout: moving from 74fca6a42863ffacaf7ba6f1936a9f228950f657 to master
74fca6a HEAD@{8 months ago}: checkout: moving from master to v2.6.31
24bc734 HEAD@{8 months ago}: pull : Fast forward
964fe08 HEAD@{11 months ago}: checkout: moving from 4a6908a3a050aacc9c3a2f36b276b46c0629ad91 to master
4a6908a HEAD@{11 months ago}: checkout: moving from master to v2.6.28

最后一个注意: --all 标志(实际上是 git-reflog 理解的 git-log 标志)将显示 refs/ 中所有已知引用的引用日志(而不是简而言之,HEAD),它将清楚地向您显示分支事件:

git reflog --date=local --all
860e4e4 refs/heads/master@{Sun Sep 19 23:00:30 2010}: commit: Second.
17695bc refs/heads/example_branch@{Mon Sep 20 00:31:06 2010}: branch: Created from HEAD

As pointed out in the comments and in Jackub's answer, as long as your branch is younger than the number of days set in the config setting gc.reflogexpire (the default is 90 days), then you can utilize your reflog to find out when a branch reference was first created.

Note that git reflog can take most git log flags. Further note that the HEAD@{0} style selectors are effectively notions of time and, in fact, are handled (in a hacked sort of way) as date strings. This means that you can use the flag --date=local and get output like this:

$ git reflog --date=local
763008c HEAD@{Fri Aug 20 10:09:18 2010}: pull : Fast-forward
f6cec0a HEAD@{Tue Aug 10 09:37:55 2010}: pull : Fast-forward
e9e70bc HEAD@{Thu Feb 4 02:51:10 2010}: pull : Fast forward
836f48c HEAD@{Thu Jan 21 14:08:14 2010}: checkout: moving from master to master
836f48c HEAD@{Thu Jan 21 14:08:10 2010}: pull : Fast forward
24bc734 HEAD@{Wed Jan 20 12:05:45 2010}: checkout: moving from 74fca6a42863ffacaf7ba6f1936a9f228950f657 
74fca6a HEAD@{Wed Jan 20 11:55:43 2010}: checkout: moving from master to v2.6.31
24bc734 HEAD@{Wed Jan 20 11:44:42 2010}: pull : Fast forward
964fe08 HEAD@{Mon Oct 26 15:29:29 2009}: checkout: moving from 4a6908a3a050aacc9c3a2f36b276b46c0629ad91 
4a6908a HEAD@{Mon Oct 26 14:52:12 2009}: checkout: moving from master to v2.6.28

It may also be useful at times to use --date=relative:

$ git reflog --date=relative
763008c HEAD@{4 weeks ago}: pull : Fast-forward
f6cec0a HEAD@{6 weeks ago}: pull : Fast-forward
e9e70bc HEAD@{8 months ago}: pull : Fast forward
836f48c HEAD@{8 months ago}: checkout: moving from master to master
836f48c HEAD@{8 months ago}: pull : Fast forward
24bc734 HEAD@{8 months ago}: checkout: moving from 74fca6a42863ffacaf7ba6f1936a9f228950f657 to master
74fca6a HEAD@{8 months ago}: checkout: moving from master to v2.6.31
24bc734 HEAD@{8 months ago}: pull : Fast forward
964fe08 HEAD@{11 months ago}: checkout: moving from 4a6908a3a050aacc9c3a2f36b276b46c0629ad91 to master
4a6908a HEAD@{11 months ago}: checkout: moving from master to v2.6.28

One last note: the --all flag (which is really a git-log flag understood by git-reflog) will show the reflogs for all known refs in refs/ (instead of simply, HEAD) which will show you branch events clearly:

git reflog --date=local --all
860e4e4 refs/heads/master@{Sun Sep 19 23:00:30 2010}: commit: Second.
17695bc refs/heads/example_branch@{Mon Sep 20 00:31:06 2010}: branch: Created from HEAD
盗梦空间 2024-08-28 12:53:52

使用

git show --summary `git merge-base foo master`

如果您希望使用 gitk 在上下文中查看它,则使用

gitk --all --select-commit=`git merge-base foo master`

(其中 foo 是您要查找的分支的名称。)

Screenshot

Use

git show --summary `git merge-base foo master`

If you’d rather see it in context using gitk, then use

gitk --all --select-commit=`git merge-base foo master`

(where foo is the name of the branch you are looking for.)

Screenshot

蓝咒 2024-08-28 12:53:52

Pro Git § 3.1 Git 分支 - 什么是分支Is 很好地解释了 Git 分支的真正含义:

Git 中的分支只是一个指向 [a] 提交的轻量级可移动指针。

由于分支只是一个轻量级指针,因此 Git 没有任何关于其历史记录或创建日期的明确概念。 “但是等等,”我听到你说,“Git 当然知道我的分支历史!”嗯,有点像。

如果您运行以下任一命令:

git log <branch> --not master
gitk <branch> --not master

您将看到看起来像“分支的历史记录”的内容,但它实际上是可从“分支”访问但无法从master访问的提交列表。这会为您提供所需的信息,但当且仅当您从未将“branch”合并回master,并且自创建以来从未将master合并到“branch”中它。如果你们合并了,那么这种差异的历史就会崩溃。

幸运的是,转发日志通常包含您想要的信息,正如此处的各种其他答案中所解释的那样。使用此:

git reflog --date=local <branch>

显示分支的历史记录。此列表中的最后一项(可能)是您创建分支的点。

如果分支已被删除,则“branch”不再是有效的 git 标识符,但您可以使用它来代替,这可能会找到您想要的内容:

git reflog --date=local | grep <branch>

或者在 Windows cmd shell 中:

git reflog --date=local | find "<branch>"

请注意,reflog 在远程分支上无法有效工作,仅限您在本地处理过的内容。

Pro Git § 3.1 Git Branching - What a Branch Is has a good explanation of what a Git branch really is:

A branch in Git is simply a lightweight movable pointer to [a] commit.

Since a branch is just a lightweight pointer, Git doesn't have any explicit notion of its history or creation date. "But hang on," I hear you say, "of course Git knows my branch history!" Well, sort of.

If you run either of the following:

git log <branch> --not master
gitk <branch> --not master

you will see what looks like the "history of your branch", but it is really a list of commits reachable from 'branch' that are not reachable from master. This gives you the information you want, but if and only if you have never merged 'branch' back to master, and have never merged master into 'branch' since you created it. If you have merged, then this history of differences will collapse.

Fortunately, the reflog often contains the information you want, as explained in various other answers here. Use this:

git reflog --date=local <branch>

to show the history of the branch. The last entry in this list is (probably) the point at which you created the branch.

If the branch has been deleted then 'branch' is no longer a valid git identifier, but you can use this instead, which may find what you want:

git reflog --date=local | grep <branch>

Or in a Windows cmd shell:

git reflog --date=local | find "<branch>"

Note that reflog won't work effectively on remote branches, only ones you have worked on locally.

随风而去 2024-08-28 12:53:52

首先,如果您的分支是在 gc.reflogexpire 天内创建的(默认 90 天,即大约三个月),您可以使用 git log -ggit reflog show 查找 reflog 中的第一个条目,这将是创建事件,如下所示(对于 git log -g

Reflog: <branch>@{<nn>} (C R Eator <[email protected]>)
Reflog message: branch: Created from <some other branch>

:会得到谁创建了一个分支,之前有多少次操作,以及来自哪个分支(好吧,它可能只是“从 HEAD 创建”,这没有多大帮助)。

这就是 MikeSep 在他的回答中所说的


其次,如果您的分支时间超过 gc.reflogexpire 并且已运行 git gc (或者它是自动运行的),则必须找到该分支的共同祖先它是从创建的。看一下配置文件,也许有 branch..merge 条目,它会告诉你这个分支基于哪个分支。

例如,如果您知道相关分支是从 master 分支创建的(从 master 分支分叉),则可以使用以下命令查看共同祖先:

git show $(git merge-base <branch> master)

您还可以尝试 git show-branch; master,作为替代方案。

这就是gbacon在他的回复中所说的

First, if your branch was created within gc.reflogexpire days (default 90 days, i.e., around three months), you can use git log -g <branch> or git reflog show <branch> to find the first entry in reflog, which would be the creation event, and looks something like below (for git log -g):

Reflog: <branch>@{<nn>} (C R Eator <[email protected]>)
Reflog message: branch: Created from <some other branch>

You would get who created a branch, how many operations ago, and from which branch (well, it might be just "Created from HEAD", which doesn't help much).

That is what MikeSep said in his answer.


Second, if you have branch for longer than gc.reflogexpire and you have run git gc (or it was run automatically), you would have to find common ancestor with the branch it was created from. Take a look at config file, perhaps there is branch.<branchname>.merge entry, which would tell you what branch this one is based on.

If you know that the branch in question was created off master branch (forking from master branch), for example, you can use the following command to see common ancestor:

git show $(git merge-base <branch> master)

You can also try git show-branch <branch> master, as an alternative.

This is what gbacon said in his response.

亽野灬性zι浪 2024-08-28 12:53:52

我还不确定它的 Git 命令,但我想你可以在引用日志中找到它们。

.git/logs/refs/heads/<yourbranch>

我的文件中似乎有 Unix 时间戳。

打印日志时似乎有一个选项可以使用引用日志历史记录而不是提交历史记录:

git log -g

您也可以跟踪此日志,回到创建分支时。不过,git log 显示的是提交日期,而不是您执行在引用日志中添加条目的操作的日期。除了查看上面路径中的实际引用日志之外,我还没有找到这一点。

I'm not sure of the Git command for it yet, but I think you can find them in the reflogs.

.git/logs/refs/heads/<yourbranch>

My files appear to have a Unix timestamp in them.

There appears to be an option to use the reflog history instead of the commit history when printing the logs:

git log -g

You can follow this log as well, back to when you created the branch. git log is showing the date of the commit, though, not the date when you made the action that made an entry in the reflog. I haven't found that yet, except by looking in the actual reflog in the path above.

椵侞 2024-08-28 12:53:52

试试这个:

  git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)'

Try this:

  git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)'
梦中的蝴蝶 2024-08-28 12:53:52

为我做到了:(10年后)

git log [--remotes] --no-walk --decorate

由于没有存储有关分支创建时间的信息,所以它的作用是显示每个分支的第一次提交(--no-walk< /code>),其中包括提交日期。对于远程分支使用 --remotes ,对于本地分支则省略它。

由于我在创建另一个分支之前至少在一个分支中进行了一次提交,因此这允许我出于文档目的追溯几个月的分支创建(以及功能开发启动)。

来源: stackexchange 上的 AnoE

This did it for me: (10 years later)

git log [--remotes] --no-walk --decorate

Since there is no stored information on branch creation times, what this does is display the first commit of each branch (--no-walk), which includes the date of the commit. Use --remotes for the remote branches, or omit it for local branches.

Since I do at least one commit in a branch before creating another one, this permitted me trace back a few months of branch creations (and feature dev-start) for documentation purposes.

source: AnoE on stackexchange

小傻瓜 2024-08-28 12:53:52

用途:

git reflog

显示当前文件夹中存储库的所有生命周期。
首先出现的分支名称(从下到上)是创建的源。

855a3ce HEAD@{0}: checkout: moving from development to feature-sut-46
855a3ce HEAD@{1}: checkout: moving from feature-sut-46 to development
855a3ce HEAD@{2}: checkout: moving from feature-jira35 to feature-sut-46
535dd9d HEAD@{3}: checkout: moving from feature-sut-46 to feature-jira35
855a3ce HEAD@{4}: checkout: moving from development to feature-sut-46
855a3ce HEAD@{5}: checkout: moving from feature-jira35 to development
535dd9d HEAD@{6}: commit: insert the format for vendor specific brower - screen.css
855a3ce HEAD@{7}: checkout: moving from development to feature-jira35
855a3ce HEAD@{8}: checkout: moving from master to development

这意味着:

  • 从 master 创建分支开发(checkout -b)

  • 从开发创建分支 feature-jira35 (checkout -b)

  • 从开发中创建分支 feature-jira-sut-46 (checkout -b)

Use:

git reflog

to show all the living cycles of your repository in the current folder.
The branch name that first appears (from down to up) is the source that was created.

855a3ce HEAD@{0}: checkout: moving from development to feature-sut-46
855a3ce HEAD@{1}: checkout: moving from feature-sut-46 to development
855a3ce HEAD@{2}: checkout: moving from feature-jira35 to feature-sut-46
535dd9d HEAD@{3}: checkout: moving from feature-sut-46 to feature-jira35
855a3ce HEAD@{4}: checkout: moving from development to feature-sut-46
855a3ce HEAD@{5}: checkout: moving from feature-jira35 to development
535dd9d HEAD@{6}: commit: insert the format for vendor specific brower - screen.css
855a3ce HEAD@{7}: checkout: moving from development to feature-jira35
855a3ce HEAD@{8}: checkout: moving from master to development

That means:

  • Branch development is created (checkout -b) from master

  • Branch feature-jira35 is created (checkout -b) from development

  • Branch feature-jira-sut-46 is created (checkout -b) from development

小情绪 2024-08-28 12:53:52

如何通过 GitHub GUI 知道

我显示了所有答案,但没有人通过 UI 给出答案。如果有人想通过 GitHub UI 查看分支何时创建。

  1. 转到见解选项卡。
  2. 选择边栏中的“网络”选项卡。

您可以看到分支创建如下

在此处输入图像描述

How to know via the GitHub GUI

I show all the answers and no one gave an answer with the UI. If anyone likes to see when the branch is created via the GitHub UI.

  1. Go to the Insights Tab.
  2. Select the Network tab in the sidebar.

You can see the branch creation like as below

Enter image description here

许久 2024-08-28 12:53:52

句法:
<代码>
git reflog --date=本地 | grep 结帐: | grep ${当前分支} |尾部-1
示例


<代码>
git reflog --date=本地 | grep 结帐: | grep dev-2.19.0 | grep dev-2.19.0 | grep dev-2.19.0尾部-1
结果


<代码>
cc7a3a8ec HEAD@{Wed Apr 29 14:58:50 2020}:结帐:从 dev-2.18.0 移动到 dev-2.19.0

syntax:

git reflog --date=local | grep checkout: | grep ${current_branch} | tail -1

example:

git reflog --date=local | grep checkout: | grep dev-2.19.0 | tail -1

result:

cc7a3a8ec HEAD@{Wed Apr 29 14:58:50 2020}: checkout: moving from dev-2.18.0 to dev-2.19.0

橘虞初梦 2024-08-28 12:53:52

此命令显示 main 中分支 dev 的创建日期:

git reflog show --date=iso dev

输出:

7a2b33d dev@{2012-11-23 13:20:28 -2100}: branch: Created from main

This command shows the creation date of branch dev from main:

git reflog show --date=iso dev

Output:

7a2b33d dev@{2012-11-23 13:20:28 -2100}: branch: Created from main
沉默的熊 2024-08-28 12:53:52

这是我在发现这个问题之前想到的。

git reflog show --date=local --all | sed 's!^.*refs/!refs/!' | grep '/master' | tail -1
git reflog show --date=local --all | sed 's!^.*refs/!refs/!' | grep 'branch:'

This is something that I came up with before I found this question.

git reflog show --date=local --all | sed 's!^.*refs/!refs/!' | grep '/master' | tail -1
git reflog show --date=local --all | sed 's!^.*refs/!refs/!' | grep 'branch:'
鲜血染红嫁衣 2024-08-28 12:53:52

我找到了最好的方法:
我总是检查通过这种方式创建的最新分支

git for-each-ref --sort=-committerdate refs/heads/

I found the best way:
I always check the latest branch created by this way

git for-each-ref --sort=-committerdate refs/heads/
想念有你 2024-08-28 12:53:52

结合 Andrew Sohn 的回答< /a>:

branchcreated=$(git reflog show --date=format:'%Y-%m-%d %H:%M:%S' --all | sed 's!^.*refs/!refs/!' | grep '/master' | tail -1| cut -d'{' -f 2| cut -d'}' -f 1 | xargs)
echo $branchcreated

Combined with the answer from Andrew Sohn:

branchcreated=$(git reflog show --date=format:'%Y-%m-%d %H:%M:%S' --all | sed 's!^.*refs/!refs/!' | grep '/master' | tail -1| cut -d'{' -f 2| cut -d'}' -f 1 | xargs)
echo $branchcreated
数理化全能战士 2024-08-28 12:53:52

如果您想获取所有分支机构的详细信息

for i in `git branch -r | tail -n +2 ` ; do git log --reverse $i | grep -A 2 -B 2 `echo $i | awk -F'origin/' '{print $2}'` | head -n 4 ; done

If you want to get the details for all the branches

for i in `git branch -r | tail -n +2 ` ; do git log --reverse $i | grep -A 2 -B 2 `echo $i | awk -F'origin/' '{print $2}'` | head -n 4 ; done
傲性难收 2024-08-28 12:53:52

如果只是检查原始修订版的 SHA-1 哈希值:

在此处输入图像描述

来源

它可以简化并使用 git log -- oneline master..修复

If it is just checking the SHA-1 hash value of the original revision:

Enter image description here

(Source)

It could be simplified and use git log --oneline master..fix.

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