FrankyBoy
FrankyBoy

Reputation: 1946

Restricting output of "git branch" to certain time period

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?

Upvotes: 2

Views: 414

Answers (1)

Kristján
Kristján

Reputation: 18803

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

Upvotes: 2

Related Questions