2

I am looking for an option to restrict the output of git branch to a certain time period. Say, I want all non-merged branches that were still active in the last two months. I can get all branches that are not merged easily with

git branch -r --no-merged origin/master 

but how would I go on filtering those by age of last commit?

FrankyBoy
  • 1,536
  • 1
  • 15
  • 31

1 Answers1

2

There are a couple other answers we can build off of:

The second one contains a good incantation. The subcommand is basically what you have so far, and then for-each-ref orders and formats the branches.

git for-each-ref \
  --sort=-committerdate \
  --format="%(committerdate:short) %(refname:short)" \
  $(git branch -r --no-merged origin/master | sed -e 's#^ *#refs/remotes/#')
2015-11-12 origin/something
2015-10-02 origin/another_thing
2015-10-01 origin/so_many_things
2015-09-30 origin/an_older_thing
2014-09-14 origin/a_really_old_thing

From there, you can filter for just the last 2 months of changes. There's a nice awk solution from this question:

... | awk '$0 > "2015-10-01"'

And then so we don't have to figure out what 2 months ago is every time, date can help us out.

date --date='-2 months' +'%Y-%m-%d'
2015-10-01

So, altogether, we have:

git for-each-ref \
  --sort=-committerdate \
  --format="%(committerdate:short) %(refname:short)" \
  $(git branch -r --no-merged origin/master | sed -e 's#^ *#refs/remotes/#') |\
awk "\$0 > \"$(date --date='-2 months' +'%Y-%m-%d')\""
2015-11-12 origin/something
2015-10-02 origin/another_thing
2015-10-01 origin/so_many_things
Kristján
  • 16,286
  • 5
  • 41
  • 56
  • Nice one. I also found those other answers but I am a big fat noob with most command line tools so had no clue how to extend them ;) – FrankyBoy Dec 02 '15 at 09:56