2

It seems like 1836 branches exist in one of the company's repos and I've been given a task to first display and then delete all branches that have not been committed to for 6 months.

I found this SO question and tried running (with both --until and --before and "month"):

#!/bin/bash
branches_to_delete_count=0
for k in $(git branch -a | sed /\*/d); do
  if [ -n "$(git log -1 --before='6 month ago' -s $k)" ]; then
    echo "NOT REALLY DELETING, git branch -D $k"
  fi
  ((branches_to_delete_count=branches_to_delete_count+1))
done
echo "Found $branches_to_delete_count branches to delete!"

But to no avail, I get the same number of branches to delete each time which is 1836.

What am I doing wrong? How can I list all branches that haven't been committed for more than 6 months?

Itai Ganot
  • 5,873
  • 18
  • 56
  • 99
  • 1
    Try `git for-each-ref --format='%(objectname) %(objecttype) %(refname) %(creatordate:unix)' --sort='-creatordate' refs/heads/` and parse the last column as seconds since Unix epoch. – phd Jun 25 '20 at 12:17
  • Does this answer your question? [git log --before="4 months" show me branches that have commits from 3 weeks ago. what am I doing wrong?](https://stackoverflow.com/questions/61585601/git-log-before-4-months-show-me-branches-that-have-commits-from-3-weeks-ago) – LeGEC Jun 25 '20 at 12:24

2 Answers2

1

The reason why all your branches show up : git log branch does not look at branch's head only, it looks at its whole history.

git log -1 --before='6 month ago' branch will :

  • unroll the history of branch
  • keep only commits older than 6 month
  • keep the first of these commits

Since (in your company's repo) all branches have a commit that is at least 6 month old in their history, git log -1 --before='6 month ago' branch will always show one line.


You can either restrict the range of commits to "a range which contains only the head commit" :

git log -1 --before='6 month ago' branch^..branch

or use git for-each-ref as @phd suggested in his comment :

git for-each-ref --format="%(refname) %(creatordate)" --sort='-creatordate' refs/heads/

and keep the branches with old enough dates.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
-1

we have not direct get the branch name which has last commit in 6 month ago so we combine git command and make shell script

Here we are passing two git command

  • first was git branch | sed s/^..// here get branch and remove two space
  • second was git log -1 --before='6 month ago' <branch-name>

pass following command in terminal copy and past in terminal give branch name

for branch in `git branch | sed s/^..//` ; do log=`git log -1 --before='6 month ago' $branch`; if [ ${#log} -gt 0 ] ; then echo $branch; fi; done

this is shell script along with git command same as above

save shell script with test.sh,change mode chmod +x test.sh then run bash test.sh

month=6 #check how many year ago
for branch in `git branch | sed s/^..//`  #get branch one by one 
do
  log=`git log -1 --before='%s month ago'$month $branch` #getting log of the branch last commit base on month
  if [ ${#log} -gt 0 ] #check if  log has output then it has branch commit before specify month ago 
  then 
      echo $branch  # print branch name which is in our project
  fi
done

let me know does it work or not

l.b.vasoya
  • 1,188
  • 8
  • 23