Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4085508
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:35:20+00:00 2026-05-20T18:35:20+00:00

My organization is using git branching extensively. As a result, we have produced over

  • 0

My organization is using git branching extensively. As a result, we have produced over 2000 branches in the past year. We are now trying to adopt a strategy for cleaning up all the old branches that are of some given age. I know how to delete branches, but I can’t find a straightforward way to list all of the branches with heads of a given age. The plan is to set up a cron that periodically deletes all branches of a given age, except those that are on some list.

Has anyone tried anything like this before?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:35:21+00:00Added an answer on May 20, 2026 at 6:35 pm

    The answers using committer dates are a good direction… if you want to delete branches that point to old commits. But you might want to delete branches which are actually old; if you create a branch today pointing to a commit from last year, you don’t want it wiped!

    So, you want to examine the reflog dates instead.

    You can get a human-readable form with git reflog show --date=local mybranch:

    8b733bc mybranch@{Tue Mar 22 13:21:49 2011}: commit: foo
    7e36e81 mybranch@{Tue Mar 22 13:21:25 2011}: commit: bar
    99803da mybranch@{Tue Mar 22 13:20:45 2011}: branch: Created from otherbranch
    

    (You might also like --date=relative)

    The entry on the top is the most recent thing that happened on that branch, so that’s the bit we care about. Unfortunately, there’s no log format placeholder for just the date, so to grab out just the date, we do a little work:

    git log -g -n 1 --date=local --pretty=%gd mybranch | sed 's/.*{\(.*\)}/\1/'
    # Prints "Mon Mar 21 13:23:26 2011"
    

    Of course, for scripting, that’s not very useful, so let’s go ahead and get the epoch time instead:

    git log -g -n 1 --date=raw --pretty=%gd mybranch | sed 's/.*{\(.*\) .*/\1/'
    # Prints 1300731806
    

    Now we’re getting somewhere!

    #!/bin/bash
    cutoff_date=$(date --date="July 23, 2010" +%s)
    git for-each-ref refs/heads --format='%(refname)' | while read branch; do
        reflog_date=$(git log -g -n 1 --date=raw --pretty=%gd $branch -- | sed 's/.*{\(.*\) .*/\1/')
        if [ -n "$reflog_date" && "$reflog_date" -lt "$cutoff_date" ]; then
            git branch -D ${branch#refs/heads/}
        fi
    done
    

    An example script! I used date to convert a human-readable date for the cutoff, then for each branch, I checked if the reflog’s last date was before the cutoff, and if so, deleted the branch. You could add in a check against a whitelist there, to save yourself from accidentally deleting something you care about. (Edit: if the branches are older than 90 days, this won’t delete them, because their reflogs will already be empty… up to you what you want to do in that case, really. You could fall back to checking the committer date, which ought to be pretty safe at that point.)

    Edit: Here’s another approach. Expire the reflogs at the cutoff time, then delete the branches whose reflogs are empty. The problem here is that if the cutoff time is older than the time when your reflogs already expire (90 days) it’ll really just be deleting branches older than 90 days instead. You could work around that, of course.

    #!/bin/bash
    
    # Git lets you use very readable time formats!
    cutoff_time="1 year ago"
    # other examples:
    # cutoff_time="July 23, 2010"
    # cutoff_time="yesterday"
    
    git for-each-ref refs/heads --format='%(refname)' | egrep -v 'master|other-whitelisted-branch' |
    while read branch; do
        git reflog expire --expire="$cutoff_time" $branch
        if [ "$(git reflog show -1 $branch | wc -l)" -eq 0 ]; then
            git branch -D ${branch#refs/heads/}
        fi
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I develop applications using the ASP.NET framework for my academic organization. We also have
I am using Maven2. I have created a parent POM for my organization. I
Have a wiki installed in our organization, and want to start using it. Failed
I'm trying to write a query using these four simplified tables: Organization (pk) OrganizationID
Right now A website for NON PROFIT organization developed using ALL FREE open source
im using filesharing in my app, working fine, but I want now for organization
I'm using ado.net entity data model. I have 2 object User Organization My problem
My organization is considering using Jabber as an agnostic device to device to application
I am using SQL Query and below are the tables. Organization OrgID Name RAOGID
I am using SQL Query and below are the tables. Organization OrgID Name RAOGID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.