找出 git 分支的创建时间(不是该分支的第一次提交)

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

find out when a git branch was created (not the first commit to that branch)

gitgit-branch

提问by David Portabella

how can I know when a git branch was created?

我怎么知道 git 分支是什么时候创建的?

i don't want to know when was the first commit to that branch. I want to find out when that branch was created.

我不想知道第一次提交到那个分支是什么时候。我想知道那个分支是什么时候创建的。

This is a script to reproduce a working example:

这是一个重现工作示例的脚本:

#! /bin/bash
set -x
set -e

mkdir test
cd test
git init
echo "hello" >readme
git add readme
git commit -m "initial import"
date

sleep 5
git checkout -b br1
date                   # this is the date that I want to find out.

sleep 5
echo "hello_br1" >readme
git commit -a -m "hello_br1"
date

echo "hello_br1_b" >readme
git commit -a -m "hello_br1_b"

git checkout master
echo "hello_master" >readme
git commit -a -m "hello_master"

git branch -a; 
git log --all --graph --abbrev-commit --decorate --pretty=format:"%h - %an, %ad : %s" --date=iso

Executing this:

执行这个:

./test.sh 
++ set -e
++ mkdir test
++ cd test
++ git init
Initialized empty Git repository in /test_git/test2/.git/
++ echo hello
++ git add readme
++ git commit -m 'initial import'
[master (root-commit) 9b95944] initial import
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 readme
++ date
Fri Aug 16 17:51:24 CEST 2013
++ sleep 5
++ git checkout -b br1
Switched to a new branch 'br1'
++ date
Fri Aug 16 17:51:29 CEST 2013
++ sleep 5
++ echo hello_br1
++ git commit -a -m hello_br1
[br1 6c559cd] hello_br1
 1 files changed, 1 insertions(+), 1 deletions(-)
++ date
Fri Aug 16 17:51:34 CEST 2013
++ echo hello_br1_b
++ git commit -a -m hello_br1_b
[br1 5f0d8ab] hello_br1_b
 1 files changed, 1 insertions(+), 1 deletions(-)
++ git checkout master
Switched to branch 'master'
++ echo hello_master
++ git commit -a -m hello_master
[master 2ed092d] hello_master
 1 files changed, 1 insertions(+), 1 deletions(-)
++ git branch -a
  br1
* master
++ git log --all --graph --abbrev-commit --decorate '--pretty=format:%h - %an, %ad : %s' --date=iso
* 5f0d8ab - David Portabella, 2013-08-16 17:51:34 +0200 : hello_br1_b
* 6c559cd - David Portabella, 2013-08-16 17:51:34 +0200 : hello_br1
| * 2ed092d - David Portabella, 2013-08-16 17:51:34 +0200 : hello_master
|/  
* 9b95944 - David Portabella, 2013-08-16 17:51:24 +0200 : initial import

so, with git log, or git reflog, I can find out the date of the initial import (17:51:24) and the date of the first commit to the branch br1 (17:51:34).

因此,使用 git log 或 git reflog,我可以找出初始导入的日期 (17:51:24) 和第一次提交到分支 br1 的日期 (17:51:34)。

but I need to find out when the branch br1 was created (17:51:29).

但我需要找出分支 br1 的创建时间(17:51:29)。

how to do that?

怎么做?

(bonus question: and, does it have a hash? how to know who created that branch)

(额外的问题:它有哈希值吗?如何知道谁创建了那个分支)

回答by John Szakmeister

Sorry, but Git doesn't keep officially tracked information of when branches are created (it's not data that stored and shared among repositories). Branches are simply references to commits and nothing more. This also means that there is no id or object that will point you at this data.

抱歉,Git 不会保留有关何时创建分支的官方跟踪信息(这不是在存储库之间存储和共享的数据)。分支只是对提交的引用,仅此而已。这也意味着没有 id 或对象可以将您指向此数据。

The reflog does keep track of when changes are made to a branch, but it's only a limited history that expires over time. It does record some information though. For instance, git branch barresulted in this entry in the reflog:

reflog 确实会跟踪对分支进行更改的时间,但它只是一个有限的历史记录,会随着时间的推移而过期。不过它确实记录了一些信息。例如,git branch bar导致引用日志中的此条目:

:: git reflog show --date=iso bar
7d9b83d bar@{2013-08-16 12:23:28 -0400}: branch: Created from master

I also see a similar entry when using git checkout -b bar:

使用时我也看到类似的条目git checkout -b bar

:: git co -b bar
Switched to a new branch 'bar'
:: git reflog show --date=iso bar
d6970ef bar@{2013-08-16 12:30:50 -0400}: branch: Created from HEAD

So, depending on your use-case, and how far back you need to dig, git reflogmay actually be useful for you.

因此,根据您的用例以及您需要挖掘多远,git reflog实际上可能对您有用。

回答by Nevik Rehnel

You can neither find out who created a branch, nor when it was created -- at least not with Git itself.

你既不能知道谁创建了一个分支,也不能知道它是什么时候创建的——至少用 Git 本身不行。

Because Git does not track branch metadata.It simply does not care about who made a branch (you usually get lots of branches from remotes), since branches are just pointers (refs) to commits.

因为Git 不跟踪分支元数据。它根本不关心谁创建了一个分支(你通常从远程获得很多分支),因为分支只是提交的指针(引用)。

Thus a branch also does nothave a branch -- a Git ref is, in fact, simply a plain text file in your .gitfolder containing the hash of the object it references (or, in case of a symbolic ref, the name of the other ref it references).

因此,一个分支也并没有有一个分支-一个Git裁判是的,其实,你在一个简单的纯文本文件.git夹包含对象的哈希引用它(或者,在一个象征性的裁判,其他的名字的情况下,参考它引用)。