1. Overview

In software development, Git is an essential tool that enables multiple developers to work on the same project’s code simultaneously and track changes. It keeps a history of all the modifications made to the code i.e., when changes were made, who modified it, what exactly was changed, and many more. While working on a project in Git, developers create and work on several branches simultaneously for various reasons. These reasons include testing and working on new features, isolating changes without affecting the main code, etc. We might need to find the branch creation date to track the history of modifications and understand the development timeline.

In this tutorial, we'll explore various commands to determine when a Git branch was created.

2. Introduction to Git Branches

Git branches are lightweight movable pointers to a particular commit. The default branch is generally master or main. When we create a branch, Git creates a new reference or pointer to the current commit in the repository. This enables us to work on specific features and fixes without affecting the main codebase. Branches help keep the work organized, allow collaboration with other developers, and enable independent testing of changes. When we finish our changes, we merge our branch back into the main branch.

Moreover, we can determine when we created a particular branch using various Git commands i.e., git reflog, git log, git for-each-ref, or via the Gitk browser. However, when we pull down a branch someone else pushed, the local creation date corresponds to when we fetched it from the remote repository. Essentially, it reflects when we obtained the branch locally.

3. Using the git reflog Command

The git reflog command keeps the history of modifications to the tip of branches and other references. It shows the changes to the repository. We can use this command with the --date option to determine when a branch was created locally.

Let's use the git reflog command with the --date=local option to check the creation date of the master branch:

$ git reflog --date=local master
1b5a4a1 (HEAD -> master) master@{Tue May 14 00:28:26 2024}: commit (initial): 2 files added

The output shows the master branch's creation date in the local time zone i.e., Tue May 00:28:26 2024.

Similarly, we can find out any other branch's creation date such as the delta branch by specifying its name:

$ git reflog --date=local delta
3de4a09 (delta) delta@{Fri Jun 7 23:52:36 2024}: commit: file1.php added
1b5a4a1 (HEAD -> master) delta@{Fri Jun 7 23:51:49 2024}: branch: Created from master

As a result, we can see the creation date and time of the delta branch.

Moreover, we can also use the relative format to display the date relative to the current time. Let's check the relative creation date of the master branch:

$ git reflog --date=relative master
1b5a4a1 (HEAD -> master) master@{4 weeks ago}: commit (initial): 2 files added

Thus, we get the output which shows that the master branch was created 4 weeks ago.

4. Using the git log Command

The git log command displays a detailed history of all the commits in the Git repository. It shows the information including commit hashes, commit messages, author, date, etc. We can use different options, such as --walk-reflogs and --reverse with the git log command to find the creation date of Git branches.

4.1. git log --walk-reflogs Command

The git log --walk-reflogs command shows the reflog history of Git branches that helps us determine their creation date.

Let's check out the branch creation date using the git log --walk-reflogs command:

$  git log --walk-reflogs
commit 1b5a4a1816d2ef960dc7aa4a0d79d567ddf246ef (HEAD -> master)
Reflog: HEAD@{0} (Laiba Younas <[email protected]>)
Reflog message: checkout: moving from delta to master
Author: Laiba Younas <[email protected]>
Date:   Tue May 14 00:28:26 2024 +0500

    2 files added

commit 3de4a0901800185b120458fdc9e288a0210c3ab9 (delta)
Reflog: HEAD@{1} (Laiba Younas <[email protected]>)
Reflog message: commit: file1.php added
Author: Laiba Younas <[email protected]>
Date:   Fri Jun 7 23:52:36 2024 +0500

    file1.php added

Hence, the output shows the reflog history of the master and delta branches, including their creation date. These dates represent when each commit happened. These timestamps let us estimate the Git branch's creation date but don't directly indicate the exact moment the branch was created.

Additionally, we can also use the -g option, the short version of the git log --walk-reflogs command. It shows the same output as git log --walk-reflogs.

4.2. git log --reverse Command

The git log --reverse command displays the commit history in reverse order from the oldest commit to the most recent one. This can be particularly useful to determine the branch creation date as it displays the first commit of a branch.

Let's use the git log --reverse command to display the earliest comment of the master branch which typically includes its creation date:

$ git log --reverse master
commit 1b5a4a1816d2ef960dc7aa4a0d79d567ddf246ef (HEAD -> master)
Author: Laiba Younas <[email protected]>
Date:   Tue May 14 00:28:26 2024 +0500

    2 files added

The output displays the creation date of the master branch. While the oldest commit may not be exactly when a branch was created, it should still be a good approximation.

5. Using the git for-each-ref Command

The git for-each-ref command iterates over all references, such as branches and tags in the Git repository. We can use this command with the --format option to specify the output's format to filter and list branches' information.

We specify the %(refname:short) and %(creatordate) format to get the local branches (refs/heads/) short name and their creation dates respectively:

$ git for-each-ref --format="%(refname:short) %(creatordate)" refs/heads/
alpha Tue May 14 00:30:10 2024 +0500
beta Tue May 14 00:30:40 2024 +0500
delta Fri Jun 7 23:52:36 2024 +0500
master Tue May 14 00:28:26 2024 +0500

The output lists all local branches i.e., alpha, beta, delta, and master with their short name and creation date.

6. Using Gitk

Gitk is the graphical user interface for Git that provides a visual representation of the branches and commits. It's an extension we can add to our Git installations on Windows, Linux, and macOS. We can use the gitk command with the --all option to display the detailed report with all branch details, including creation date:

$ gitk --all

Consequently, we'll get the report that shows the Git branches' names, commits, and their creation dates:

determine Git branch creation time using Gitk

Hence, we'll see the branch graph and most recent commit message in the first column, the author name and email in the second column, and the creation date of the first commit in each branch in the third column.

7. Conclusion

In this article, we've explored the different methods to determine when we created the Git branch. We used several git commands, such as git reflog, git log, git for-each-ref, and the Gitk browser to find and display the creation date of Git branches.

We can use any of these strategies to check when a particular branch was created in Git.