Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing each branch and its last revision's date in Git

I need to delete old and unmaintained branches from our remote repository. I'm trying to find a way with which to list the remote branches by their last modified date, and I can't.

Is there an easy way to list remote branches this way?

like image 434
Roni Yaniv Avatar asked Oct 17 '22 08:10

Roni Yaniv


People also ask

How can I get a list of git branches ordered by most recent commit?

Use git branch --sort=-committerdate to display a list of all local branches and sort them based on the date of their last commit. Use arrow keys to navigate, press Q to exit.

What is the date command to view all the remote branches?

The git branch -r command is sufficient if you want a brief overview of all the branches stored on a remote. If you want more detailed information, the git remote show command may be more useful. This command returns: All remote branches.


2 Answers

commandlinefu has 2 interesting propositions:

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

or:

for k in $(git branch | sed s/^..//); do echo -e $(git log --color=always -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k --)\\t"$k";done | sort

That is for local branches, in a Unix syntax. Using git branch -r, you can similarly show remote branches:

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

Michael Forrest mentions in the comments that zsh requires escapes for the sed expression:

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 

kontinuity adds in the comments:

If you want to add it your zshrc the following escape is needed.

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

In multiple lines:

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

Note: n8tr's answer, based on git for-each-ref refs/heads is cleaner. And faster.
See also "Name only option for git branch --list?"

More generally, tripleee reminds us in the comments:

  • Prefer modern $(command substitution) syntax over obsolescent backtick syntax.

(I illustrated that point in 2014 with "What is the difference between $(command) and `command` in shell programming?")

  • Don't read lines with for.
  • Probably switch to git for-each-ref refs/remote to get remote branch names in machine-readable format
like image 200
VonC Avatar answered Oct 18 '22 22:10

VonC


Here is what I use:

git for-each-ref --sort='-committerdate:iso8601' --format=' %(committerdate:iso8601)%09%(refname)' refs/heads

This is the output:

2014-01-22 11:43:18 +0100       refs/heads/master
2014-01-22 11:43:18 +0100       refs/heads/a
2014-01-17 12:34:01 +0100       refs/heads/b
2014-01-14 15:58:33 +0100       refs/heads/maint
2013-12-11 14:20:06 +0100       refs/heads/d/e
2013-12-09 12:48:04 +0100       refs/heads/f

For remote branches, just use "refs/remotes" instead of "refs/heads":

git for-each-ref --sort='-committerdate:iso8601' --format=' %(committerdate:iso8601)%09%(refname)' refs/remotes

Building on n8tr's answer, if you are also interested in the last author on the branch, and if you have the "column" tool available, you can use:

git for-each-ref --sort='-committerdate:iso8601' --format='%(committerdate:relative)|%(refname:short)|%(committername)' refs/remotes/ | column -s '|' -t

Which will give you:

21 minutes ago  refs/remotes/a        John Doe
6 hours ago     refs/remotes/b        Jane Doe
6 days ago      refs/remotes/master   John Doe

You may want to call git fetch --prune before to have the latest information.

like image 160
ocroquette Avatar answered Oct 18 '22 21:10

ocroquette