Skip to content

Instantly share code, notes, and snippets.

@mroderick
Last active April 11, 2024 19:49
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save mroderick/4472d26c77ca9b7febd0 to your computer and use it in GitHub Desktop.
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
@trntsmn
Copy link

trntsmn commented May 21, 2019

Quickly hacked together a powershell version in case it helps anyone
`#! /usr/bin/pwsh

echo ""
echo "Not merged branches"
$branches = git branch -r --no-merged | grep -v HEAD
$output = @()
foreach( $branch in $branches ) {
$branch = $branch.Trim()
$output += git log -n 1 --format="%ci, %cr, %an, %ae, $branch" --no-merges --first-parent $branch | Sort-Object
}
echo $output`

@naidubrahma
Copy link

Could you please suggest me how to list all the branches under enitirr gitlab

@renatoathaydes
Copy link

It's a good idea to change the column separators because the dates in the output contain ,, so another column separator like | is more appropriate to avoid confusion.
Just change --format="%ci, %cr, %an, %ae, " to --format="%ci | %cr | %an | %ae | ".

@odahcam
Copy link

odahcam commented Aug 31, 2021

A quick one liner to delete all merged branches: for branch in `git branch -r --merged | grep -v HEAD`;do git branch -D $branch; done

@kondr1
Copy link

kondr1 commented Aug 16, 2022

Here is my solution for powershell
https://gist.github.com/kondr1/b9252ed216bd327eb1063ad850b7c7f0
It will work faster, because it's using only one git command

@RobinBastiaan
Copy link

Please help me understand this script. How does this script filter out branches that have not seen commits in two months?

@the1337beauty
Copy link

Please help me understand this script. How does this script filter out branches that have not seen commits in two months?

From what I can tell, the command doesn't specifically point out branches that haven't had commits in 2 months but it puts the most recent commit for each branch in a readable, one-liner format. The author has split the commands for merged branches and non-merged branches. Also pointing out that they are filtering by non-merged commits so merged commits (ex. merging of branches) are excluded.

@Hrithikwmp
Copy link

Hrithikwmp commented Sep 2, 2023

ok, someone please tell me how to run this script.
i mean how this script works , it doesn't require any login or anything

@davidecavestro
Copy link

ok, someone please tell me how to run this script. i mean how this script works , it doesn't require any login or anything

It's a bash script.
You can save it into a folder where you have previously cloned a git repo.
Then run it: if you haven't prevously saved your git credentials, git will prompt for them.

@davidecavestro
Copy link

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

@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