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

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

How to determine when a Git branch was created?

gitbranchgit-branch

提问by paxos1977

Is there a way to determine whena Git branch was created? I have a branch in my repo and and I don't remember creating it and thought maybe seeing the creation timestamp would jog my memory.

有没有办法确定何时创建 Git 分支?我的 repo 中有一个分支,我不记得创建它,我想也许看到创建时间戳会唤起我的记忆。

采纳答案by Greg Bacon

Use

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

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

如果您更愿意使用 gitk 在上下文中查看它,请使用

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

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

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

Screenshot

截屏

回答by Aaron

As pointed out in the commentsand in Hymanub'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.

正如评论Hymanub 的回答指出的那样,只要您的分支比配置设置中设置的天数gc.reflogexpire(默认为 90 天)还年轻,那么您就可以利用您的 reflog 来找出分支引用的时间首先创建。

Note that git reflogcan take most git logflags. 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=localand get output like this:

请注意,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

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

有时也可能有用--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 --allflag (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:

最后一个注意事项:--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

回答by yoyo

Pro Git § 3.1 Git Branching - What a Branch Ishas a good explanation of what a git branch really is

Pro Git § 3.1 Git Branching - What a Branch Is很好地解释了 git branch 到底是什么

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

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

Since a branch is just a lightweight pointer, git has no 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.

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

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 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 havemerged, then this history of differences will collapse.

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

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:

如果分支已被删除,则 'branch' 不再是有效的 git 标识符,但您可以改用它,它可能会找到您想要的:

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

Or in a Windows cmd shell:

或者在 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.

请注意,reflog 无法在远程分支上有效工作,只有您在本地工作过的分支才有效。

回答by Jakub Nar?bski

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

首先,如果您的分支是在gc.reflogexpire几天内创建的(默认为 90 天,即大约 3 个月),您可以使用git log -g <branch>git reflog show <branch>在 reflog 中找到第一个条目,这将是创建事件,如下所示(对于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).

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

That is what MikeSep said in his answer.

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



Second, if you have branch for longer than gc.reflogexpireand 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>.mergeentry, which would tell you what branch this one is based on.

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

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:

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

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

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

您也可以尝试git show-branch <branch> master,作为替代。

This is what gbacon said in his response.

这是gbacon在他的回应中所说的

回答by Mike Seplowitz

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

我还不确定它的 git 命令,但我认为你可以在 reflogs 中找到它们。

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

My files appear to have a unix timestamp in them.

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

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

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

git log -g

You can follow this log as well, back to when you created the branch. git logis 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.

您也可以遵循此日志,回到创建分支时的状态。git log但是,显示的是提交的日期,而不是您执行在 reflog 中创建条目的操作的日期。除了查看上面路径中的实际引用日志之外,我还没有找到。

回答by Sireesh Yarlagadda

Try this

尝试这个

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

回答by De Nguyen

Use:

用:

git reflog

to show all the living cycle of your repository in current folder. The branch name that first appear (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 mean:

这意味着:

  • 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

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

  • 分支功能-jira35 是从开发中创建的 (checkout -b)

  • 分支 feature-jira-sut-46 从 development 创建(checkout -b)

回答by Andrew Sohn

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

这是我在找到这个线程之前想到的。

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:'

回答by Sazzad Hissain Khan

This commands shows the created date of branch devfrom main

此命令显示分支的创建日期devmain

$git reflog show --date=iso dev
a2b33d dev@{2012-11-23 13:20:28 -2100}: branch: Created from main

回答by Anshul Agarwal

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 }'` |head -n 4; done