Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list all branches that contain commits newer than a specific date?

When using git log, I can supply --since=<date> to limit the log to commits newer than a certain date.

With git branch -r, I get all the remote branches.

How would I get the list of branches that have contributions that are younger than a given date, i.e. all branches that contain commits newer than the date I'm interested in?

Alternatively, if that is hard or not possible, respecting only the date of the branch's tip might be sufficient.

like image 839
Martin Avatar asked Jan 30 '15 14:01

Martin


2 Answers

How would I get the list of branches that have contributions that are younger than a given date, i.e. all branches that contain commits newer than the date I'm interested in?

Here is one possible approach: use git for-each-ref to run

git log -1 --since=<date> <branch>

for each of the branch references in your repo. If the output of this git log command is non-empty, then the branch in question contains commits newer than <date>, and you should print the branch's name in your list; otherwise, it does not, and you shouldn't print its name.

Here is a shell script that takes one argument, which should be a string that Git can recognize as a date (such as 2014/12/25 13:00, 3.months.ago, yesterday etc.), and lists all the "green branches" (for lack of a better term), i.e. local branches that contain commits newer than the specified date.

#!/bin/sh

# git-greenbranch.sh
#
# List the local branches that contain commits newer than a specific date
#
# Usage: git greenbranch <date>
#
# To make a Git alias called 'greenbranch' out of this script,
# put the latter on your search path, and run
#
#   git config --global alias.greenbranch '!sh git-greenbranch.sh'

if [ $# -ne 1 ]
then
    printf "usage: git greenbranch <date>\n\n"
    printf "For more details on the allowed formats for <date>, see the\n"
    printf "'git-log' man page.\n"
    exit 1
fi

testdate=$1

git for-each-ref --format='%(refname:short)' refs/heads/ \
    | while read ref; do
          if [ -n "$(git rev-list --max-count=1 --since="$testdate" $ref)" ]
          then
              printf "%s\n" "$ref"
          fi
      done

exit $?

(The script is available at Jubobs/git-aliases on GitHub.)

For convenience, you can define, at the user level, a Git alias (called greenbranch.sh, here) that runs the script in question:

git config --global alias.greenbranch '!sh git-greenbranch.sh'

Make sure the shell script is on your path.

Tests in a clone of the Git-project repo

$ git clone https://github.com/git/git/
# go grab a cup o' coffee...

$ cd git

# check all remote branches out (for testing purposes)
$ git checkout -b maint origin/maint
$ git checkout -b next origin/next
$ git checkout -b pu origin/pu
$ git checkout -b todo origin/todo

$ git greenbranch "yesterday"
maint
master
next
pu
todo
$ git greenbranch "today"
$

This shows that all five branches contain commits made "yesterday", but none contain commits made "today".

like image 85
jub0bs Avatar answered Nov 05 '22 06:11

jub0bs


--simplify-by-decoration lists only commits that have a direct reference:

git log --oneline --decorate --branches --remotes --since=$date \
        --simplify-by-decoration

from there it's just a question of formatting.

like image 36
jthill Avatar answered Nov 05 '22 05:11

jthill