Git; Get All Branches Last Commit Date and Time

for k in git branch | perl -pe s/^..//; do echo -e git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1\\t$k; done | sort -r

The set of piped commands above produces easy-to-read output of all your repo’s branch names and their last commit dates.

Below is an explanation of the commands:

  • for k in starting of a command line loop
  • git branch list all branches in repo one at a time. Through the loop the current branch name is set to k
  • | perl -pe s/^..//`; pipe the git branch output to perl which performs a regular expression ( -e flag ) removing spaces on each line of output ( -p ) flag, so just the branch name is set to k without spaces or return characters.
  • do echo -e print to the screen every item in the for k loop
  • git show shows the change date as well as the changes of the k branch. Adding the pretty=format: allows the default date structure to be changed and coloured, making the output more legible and informative. You can read more about pretty=format: here.
  • -- | head -n 1 pipes the git show output into the head command, which just returns the first line; -n 1 ensures only the first line is shown.
  • \\t$k prints a tab and then writes the k value, which is the name of the branch.
  • ; done ends the looping.
  • | sort -r pipes the final looped output to the sort command. Sort command is somewhat magical, as it is able to deduce the beginning of each output line is a date and sort by that date. The -r flag reverses the output, making the newest branch commit date at the top.