[git] 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.

This question is related to git branch git-branch

The answer is


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


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


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

Try this

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

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

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

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

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

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.


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

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


very simple

In the address bar of your web browser, type the following URL and press the Enter key to retrieve information of a GitHub repository.

https://api.github.com/repos/{:owner}/{:repository}

Note! In the URL above, replace the {:owner} and {:repository} parts with the values that can be found in the URL of the GitHub repository for which you want to know the creation date. For example, if the URL of the repository is https://github.com/ArthurGareginyan/batch-rename/, then the URL of it’s information will be https://api.github.com/repos/ArthurGareginyan/batch-rename.

output:

image


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


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.


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

Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to branch

Get git branch name in Jenkins Pipeline/Jenkinsfile Why do I have to "git push --set-upstream origin <branch>"? Your configuration specifies to merge with the <branch name> from the remote, but no such ref was fetched.? When does Git refresh the list of remote branches? Fix GitLab error: "you are not allowed to push code to protected branches on this project"? Git push: "fatal 'origin' does not appear to be a git repository - fatal Could not read from remote repository." Git: Merge a Remote branch locally git pull from master into the development branch Depend on a branch or tag using a git URL in a package.json? How can I copy the content of a branch to a new local branch?

Examples related to git-branch

How do I rename both a Git local and remote branch name? How do I create a master branch in a bare Git repository? git switch branch without discarding local changes Git: Merge a Remote branch locally Why call git branch --unset-upstream to fixup? Create a remote branch on GitHub How can I display the current branch and folder path in terminal? Git merge master into feature branch Delete branches in Bitbucket Creating a new empty branch for a new project