375

Is there a way to determine when a 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.

paxos1977
  • 135,245
  • 26
  • 85
  • 125
  • 6
    found this that was very helpful http://www.commandlinefu.com/commands/view/2345/show-git-branches-by-date-useful-for-showing-active-branches – Brendon-Van-Heyzen Dec 17 '10 at 19:41
  • 3
    When you asked this question, were you really just interested in getting the date and time of creation of the branch, or were you also interested in knowing *where* in your commit history the branch was first created, i.e. which commit your branch was first branched off from? –  Jul 24 '14 at 18:13
  • 4
    @Cupcake, the question is pretty clear. I was interested in when I created the branch. That said knowing the commit would be handy information in the general case. – paxos1977 Jul 25 '14 at 18:49

14 Answers14

172

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

Robert Mikes
  • 945
  • 6
  • 18
Greg Bacon
  • 121,231
  • 29
  • 179
  • 236
  • 27
    To clarify the answer, there are two steps to the process. (1) get the treesh using "git merge-base master" where branch is the branch of interest. (2) Use the treesh as input into git show to get the date: "git show --summary " – paxos1977 Feb 12 '10 at 23:29
  • 12
    This answer seems to except that the branch has been created from master. But what if it's not the case ? Is there a way to find the first commit of the branch having more than 1 child ? – Manitra Andriamitondra Nov 10 '11 at 16:14
  • 27
    This is not the date at which the branch was _created_ -- this is the "branching" commit. – Marco Mar 23 '12 at 09:44
  • 49
    The solution will only work if 'branch' was never merged back to 'master'. Is there a way to find the very first merge base for two branches universally? – Ilya Ivanov Apr 16 '12 at 10:03
  • 1
    @ceretullis, I'm new to the term "treesh". Is that a mash-up of _tree_ and _hash_? – tony19 Oct 23 '12 at 06:06
  • 1
    @user46874 The term used in the git docs is tree-ish, one of many ways to identify a particular tree in a repo. See [Git Tools - Revision Selection](http://git-scm.com/book/en/Git-Tools-Revision-Selection) or [git treeishes considered awesome](http://highgroove.com/articles/2012/01/19/git-treeishes-considered-awesome.html). – Greg Bacon Oct 23 '12 at 09:22
  • 25
    This is showing the merge base, not branch creation. – Hedley Feb 28 '14 at 10:39
  • 2
    This answer assumes a lot of things, which can be not true (including branch name `master`) and doesn't answers the question – Dims Mar 15 '19 at 14:41
  • This command works only from GitBash, and does not work from Windows Cmd – Khalil Khalaf Dec 19 '19 at 19:53
161

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
Brady Dowling
  • 3,296
  • 2
  • 22
  • 43
Aaron
  • 1,977
  • 1
  • 11
  • 7
  • 3
    Very interesting. +1. Provided, of course, this takes place within `gc.reflogexpire` days. – VonC Sep 20 '10 at 06:05
  • 2
    @VonC — right you are. The default value for gc.reflogexpire is 90 days. – Aaron Sep 24 '10 at 04:09
  • 3
    It's important to note that `reflog` only pertains to a _local_ repo's history so it won't help when the branch was created elsewhere and _pulled_. In that case you can only find out when you pulled the branch. – boweeb Aug 19 '20 at 13:57
69

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 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.

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 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.

yoyo
  • 7,170
  • 4
  • 49
  • 47
  • Hmm, I'm not sure how useful this answer is yet, I'll need to study it more later. For what it's worth, though, you definitely did a good job of putting in the effort to write something **comprehensive**, and not just a short, lazy partial answer, so that's definitely good. Also, it's important to note that you can only use the reflog for this as long as your branch is not older than `gc.reflogexpire` days, as pointed out in [this answer](http://stackoverflow.com/a/2256267/456814) and [this answer](http://stackoverflow.com/a/3748722/456814). –  Jul 24 '14 at 18:03
  • 4
    I didn't want to duplicate all the good information about reflogs from the other answers, but happy to add the gc.reflogexpire if you think it's useful. My answer was intended to (1) provide more clarity on what a git branch is and why its "history" is somewhat nebulous, (2) put useful commands front and center, including (3) showing commits on a branch and not master and (4) grep-ing the reflog for a deleted branch. Feedback welcome. – yoyo Jul 24 '14 at 18:41
  • Thanks @Cupcake. Funnily enough I originally had angle brackets around 'branch' but that was stripping the entire word out of my answer preview, so I assumed it was mistakenly treated as (invalid) inline html. – yoyo Jul 24 '14 at 18:51
  • This method worked well via intellij and BitBucket `git reflog --date=local ` – isaac weathers Sep 29 '16 at 16:27
  • Thankyou very much @yoyo – Lucas Martins Feb 27 '21 at 15:51
45

First, if you branch was created within gc.reflogexpire days (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):

Reflog: <branch>@{<nn>} (C R Eator <creator@example.com>)
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.

Community
  • 1
  • 1
Jakub Narębski
  • 268,805
  • 58
  • 209
  • 228
  • 3
    "git reflog show " works well, very explicitly shows when the branch was created. Treesh feeds into "git show --summary " – paxos1977 Feb 13 '10 at 18:08
  • 1
    The 'git log -g ' was the one that worked for me - lots of detail. Need to be on the branch to use any of these. – Lidia Nov 19 '15 at 01:43
20

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.

Update: 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.

Mike Seplowitz
  • 8,197
  • 1
  • 20
  • 23
13

Try this

  git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)'
biniam
  • 7,518
  • 6
  • 42
  • 52
Sireesh Yarlagadda
  • 10,572
  • 2
  • 65
  • 71
  • 3
    you may probably need `%` before `(refname)` – Vor Sep 17 '15 at 18:35
  • 1
    @Vor I made the change – biniam Nov 28 '16 at 14:10
  • I piped this through `| cut -c 5- | sort -r |` and then piped through grep for the month, giving me a list lin reverse chronological order, more or less. – Noumenon Nov 05 '18 at 14:37
  • 2
    @Noumenon: for-each-ref can sort for you, by adding e.g. `--sort='-committerdate'` (note the '-' before committerdate for reverse chronological order). – Pete Nov 17 '18 at 18:22
10

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

De Nguyen
  • 357
  • 3
  • 4
  • 2
    but where are the dates? and you see checking-out to every branch many times. Does this mean only the FIRST occurrence each branch is its creation? – Motti Shneor Feb 07 '17 at 14:48
5

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:'
Andrew Sohn
  • 595
  • 1
  • 4
  • 13
5

This commands shows the created date of branch dev from main

$git reflog show --date=iso dev
$7a2b33d dev@{2012-11-23 13:20:28 -2100}: branch: Created from main
Sazzad Hissain Khan
  • 29,428
  • 20
  • 134
  • 192
  • "the created date of branch"... if less than 90 days. If it was created *more* than 90 days, that information would be purged. As mentioned above in https://stackoverflow.com/a/3748722/6309. – VonC Mar 06 '19 at 17:29
  • @Sazzad Hissain Khan This one worked for us as we wanted to provide 'friendly cheat-sheet tips' to some non-technical folk who were getting a little lost with some of the intricacies of Git. – Chris22 Mar 22 '19 at 14:45
4

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

SherylHohman
  • 12,507
  • 16
  • 70
  • 78
3

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

git for-each-ref --sort=-committerdate refs/heads/
user838900
  • 51
  • 2
2

Combined with the answer from Andrew Sohn (https://stackoverflow.com/a/14265207/1929406)

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
kivagant
  • 1,368
  • 1
  • 18
  • 27
2

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
1

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

Jamter
  • 305
  • 3
  • 10