Programming

Listing each branch and its last revisions date in Git

27 September 2026 · 8 min read

Listing each branch and its last revisions date in Git

In the dynamic world of software development, managing your codebase effectively is paramount. Git, as the industry-standard version control system, empowers teams to collaborate seamlessly. However, as projects grow, so does the number of branches, and keeping track of their activity becomes a real challenge. Knowing the last time a branch was updated can be crucial for identifying stale branches, optimizing workflows, and maintaining a clean repository. This article will guide you through the essential commands and techniques for listing each branch and its last revision’s date in Git, providing you with the insights needed for superior repository management and ensuring your team always has an accurate pulse on your project’s history.

Understanding Git Branches and Their Lifecycle

Git branches are fundamental to modern development workflows, allowing developers to work on new features or bug fixes in isolation without affecting the main codebase. Each branch represents an independent line of development. As work progresses, commits are added to these branches, creating a rich history of changes. Understanding the lifecycle of these branches—from creation to merging or deletion—is key to efficient version control.

Over time, a repository can accumulate numerous branches. Some are actively developed, while others might be long-forgotten feature branches or experimental forks. Identifying these inactive or “stale” branches is vital for maintaining a healthy repository. A cluttered branch list can lead to confusion, slow down operations, and even increase the risk of working on outdated code. Tools that help with Git branch history and status are invaluable.

The ability to quickly ascertain the commit date of the most recent change on any given branch provides immediate clarity. This information helps developers decide whether a branch is still relevant, if it needs to be merged, rebased, or perhaps archived. It’s a critical aspect of efficient repository management and helps ensure that resources are focused on active development lines.

Core Git Commands for Branch Insights

Git provides powerful commands to inspect your repository’s state, including detailed information about branches. The primary command for viewing branches is git branch, but to get the last commit date, we need to combine it with other options or the git log command. Mastering these commands is essential for anyone looking to gain deeper insights into their project’s branch tracking.

The basic git branch command lists all local branches. Adding the -v or --verbose flag provides more detail, showing the last commit hash and subject for each branch. This is a good starting point for listing each branch and its last revision’s date in Git, as it quickly gives you a snapshot of recent activity. However, it doesn’t directly show the date in an easily parsable format for all branches simultaneously.

For more granular control and custom formatting, the git for-each-ref command is incredibly powerful. This command allows you to iterate over all references (including branches, tags, and remotes) and print custom information using placeholders. It’s a versatile tool for scripting and automating Git information retrieval, offering far more flexibility than simple git branch variants. According to Git’s official documentation, git for-each-ref is “a plumbing command used to iterate over all refs and display information about them.”

Utilizing git for-each-ref for Comprehensive Branch Data

To specifically extract the last commit date for each branch, git for-each-ref combined with specific format placeholders is the most effective method. This approach allows you to precisely control the output, making it easy to parse or simply view. This is the go-to method for listing each branch and its last revision’s date in Git with precision.

To get the last commit date for all Git branches, you can use the command: git for-each-ref --sort='-committerdate' refs/heads/ --format='%(committerdate:short) %(refname:short)'. This command iterates through all local branches (refs/heads/), sorts them by their committer date in descending order, and then formats the output to show just the short date and the branch name. This provides a clean, sorted list of your branches and their most recent activity, which is incredibly useful for repository management.

This command not only shows local branches but can be adapted to show remote branches as well by changing refs/heads/ to refs/remotes/. Understanding this flexibility is key to comprehensive branch tracking across your entire Git environment. It allows you to monitor not just your local work, but also the state of branches on your remote repositories like GitHub or GitLab.

Infographic here
Practical Applications and Advanced Techniques ----------------------------------------------

Beyond simply viewing the data, understanding how to apply this knowledge can significantly improve your development workflow. Identifying stale branches is a prime example. Branches that haven’t been updated in months or even years might indicate abandoned features or outdated experiments. Proactive cleanup prevents repository bloat and potential confusion.

Here are some practical applications:

  • Stale Branch Identification: Quickly find branches that haven’t seen activity in a long time, making them candidates for archival or deletion.
  • Code Review Prioritization: Focus code review efforts on actively developed branches.
  • Resource Management: Identify which features or bug fixes are currently being worked on based on recent branch activity.
  • Auditing and Compliance: For regulated environments, knowing when the last change was made on a specific branch can be crucial for audit trails.

For more advanced scenarios, you might want to integrate this information into custom scripts or CI/CD pipelines. For instance, a script could automatically notify team leads about branches older than 90 days. This level of automation elevates your version control practices and ensures adherence to best practices without manual oversight.

Scripting for Automated Branch Reports

Automating the process of listing each branch and its last revision’s date in Git can save considerable time and ensure consistent reporting. You can create a simple shell script to run the git for-each-ref command and format the output for easier consumption, perhaps even exporting it to a CSV file for further analysis. This is particularly useful for large projects with many contributors and branches.

Consider a script that generates a report of all branches sorted by their last commit date, highlighting those older than a specified threshold. This proactive approach to Git branch history management helps maintain a clean and efficient repository. Such reports can be shared during team stand-ups or integrated into project dashboards, providing transparency and accountability.

For example, a script could parse the output of git for-each-ref and then use conditional logic to mark branches older than a certain date as “STALE”. This not only provides the data but also actionable insights. You can further explore Git’s extensive logging capabilities to extract more specific information, such as author details or full commit messages, as detailed in the official Git log documentation.

Troubleshooting and Best Practices

While the commands are generally straightforward, sometimes you might encounter unexpected results. For instance, a branch might show a recent commit date, but upon inspection, it’s just a merge commit from another branch, not new work. Always cross-reference with git log <branch-name> to see the full commit history if in doubt. Understanding Git log commands is key to deep dives.

Here are some best practices for effective branch management:

  1. Regular Cleanup: Periodically review your branch list and remove or archive old, merged, or abandoned branches. This helps keep your repository tidy and improves performance.
  2. Consistent Naming Conventions: Adopt clear and consistent branch naming conventions (e.g., feature/feature-name, bugfix/issue-id). This makes it easier to understand a branch’s purpose at a glance.
  3. Automate Reporting: Implement scripts to generate regular reports on branch activity. This can highlight potential issues before they become major problems.
  4. Educate Your Team: Ensure all team members understand the importance of branch hygiene and how to use these commands. Atlassian offers excellent [commandlinefu](<https:// Question & Answer :

    I need to delete old and unmaintained branches from our remote repository. I’m trying to find a way with which to list the remote branches by their last modified date, and I can’t.

    Is there an easy way to list remote branches this way?


    <a href=>) has 2 interesting propositions: ``` for k in $(git branch | perl -pe s/^..//); do echo -e $(git show –pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k – | head -n 1)\t$k; done | sort -r

    
    or:
    
    for k in $(git branch | sed s/^..//); do echo -e $(git log –color=always -1 –pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k –)\t"$k";done | sort
    
    That is for local branches, in a Unix syntax. Using `git branch -r`, you can similarly show remote branches:
    
    for k in $(git branch -r | perl -pe ’s/^..(.?)( ->.)?$/\1/’); do echo -e $(git show –pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k – | head -n 1)\t$k; done | sort -r
    
    ---
    
    [Michael Forrest](https://stackoverflow.com/users/191991/michael-forrest) mentions [in the comments](https://stackoverflow.com/questions/2514172/listing-each-branch-and-its-last-revisions-date-in-git/2514279#comment27884192_2514279) that zsh requires escapes for the `sed` expression:
    
    for k in git branch | perl -pe s/^..//; do echo -e git show –pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k – | head -n 1\t$k; done | sort -r
    
    [kontinuity](https://stackoverflow.com/users/1025068/kontinuity) adds [in the comments](https://stackoverflow.com/questions/2514172/listing-each-branch-and-its-last-revisions-date-in-git/2514279#comment65532526_2514279):
    
    > If you want to add it your zshrc the following escape is needed.
    
    alias gbage=‘for k in $(git branch -r | perl -pe ‘'’s/^..(.?)( ->.)?$/\1/’'’); do echo -e $(git show –pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k – | head -n 1)\t$k; done | sort -r’
    
    In multiple lines:
    
    alias gbage=‘for k in $(git branch -r | \ perl -pe ‘'’s/^..(.?)( ->.)?$/\1/’'’); \ do echo -e $(git show –pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k – | \ head -n 1)\t$k; done | sort -r’
    
    ---
    
    Note: [n8tr](https://stackoverflow.com/users/1202867/n8tr)'s [answer](https://stackoverflow.com/a/23943986/6309), based on [`git for-each-ref refs/heads`](https://git-scm.com/docs/git-for-each-ref) is cleaner. [And faster](https://stackoverflow.com/a/41307509/6309).  
     See also "[Name only option for `git branch --list`?](https://stackoverflow.com/a/36026316/6309)"
    
    More generally, [tripleee](https://stackoverflow.com/users/874188/tripleee) reminds us [in the comments](https://stackoverflow.com/questions/2514172/listing-each-branch-and-its-last-revisions-date-in-git#comment106493431_2514279):
    
    > - Prefer modern `$(command substitution)` syntax over obsolescent backtick syntax.
    
    (I illustrated that point in 2014 with "[What is the difference between `$(command)` and ``command`` in shell programming?](https://stackoverflow.com/a/24592916/6309)")
    
    > - [Don't read lines with `for`](https://mywiki.wooledge.org/DontReadLinesWithFor).
    > - Probably switch to `git for-each-ref refs/remote` to get remote branch names in machine-readable format