Skip to content

Instantly share code, notes, and snippets.

@mroderick
Last active June 2, 2025 19:37
Show Gist options
  • Select an option

  • Save mroderick/4472d26c77ca9b7febd0 to your computer and use it in GitHub Desktop.

Select an option

Save mroderick/4472d26c77ca9b7febd0 to your computer and use it in GitHub Desktop.
A small script to find stale branches
#!/bin/bash
# This is a very naive script, it doesn't do grouping and returns all branches
# I only really care about branches that have not seen commits in two months
#
# I am hoping to find some time to write a tool that can output these reports for me
# In the meantime, I am using this
echo "Merged branches"
for branch in `git branch -r --merged | grep -v HEAD`;do echo -e `git log --no-merges -n 1 --format="%ci, %cr, %an, %ae, " $branch | head -n 1` \\t$branch; done | sort -r
echo ""
echo "Not merged branches"
for branch in `git branch -r --no-merged | grep -v HEAD`;do echo -e `git log --no-merges -n 1 --format="%ci, %cr, %an, %ae, " $branch | head -n 1` \\t$branch; done | sort -r
@davidecavestro
Copy link
Copy Markdown

davidecavestro commented Sep 27, 2023

In order to filter with awk keeping just branches that have not seen commits in two months
it would suffice prepending %ct (committer date, UNIX timestamp) to the git log fields
then filter and strip the added prefix

Since I want to produce a CSV I did it as follows (for unmerged branches)

for branch in `git branch -r --no-merged | grep -v HEAD`;do
  echo -e $(git log --no-merges -n 1 \
    --format="%ct,%cI,%an,%ae," $branch |                    `# prepend secs since EPOCH as 1st field` \
    head -n 1)$branch; \
done | \
  sort -r | \
  awk -F , '$1 < limit' limit=$(date -d "2 month ago" +%s) | `# split by comma and filter based on 1st field` \
  sed 's/^[0-9]*\,//g'                                       `# strip 1st field now`

It can clearly be optimized based on needs, moving sort after filter and so on

PS: please note here I added comments within ` (backticks) for readability, so in turn I had to replace subshell backticks with the $() syntax.
Also note that this approach leverages some additional comands, namely awk, date and sed

@StingyJack
Copy link
Copy Markdown

@trntsmn - The term 'grep' is not recognized as a name of a cmdlet, function, script file, or executable program.
=D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment