How to determine when a Git branch was created?
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.
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.)
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
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.