Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list only active / recently changed branches in git?

I sometimes work with source code repositories containing many branches, most of which are old and usually no longer relevant.

In these cases, the full list of branches from git branch is not very helpful. Is there a way to only list "active" branches? For example, only branches that received commits in the last n days? Ideally, the list would include the last commit date for each branch, and indicate if the branch is already fully merged.

P.S.: I realize that this can also be solved by deleting "old" branches (as discussed e.g. in What to do with experimental non-merged git branches? ), but this may not always be practical or accepted on a given project.

like image 366
sleske Avatar asked Jun 21 '12 09:06

sleske


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.

How do I get a list of branches created by user?

If you add --sort=authorname to the above command you can get your branches grouped together. Otherwise I am not sure if you can list the branches only you created. This lists branches, but not by their authors. They are sorted by the author of last commit to every branch, not the branch author.


2 Answers

You can use git-for-each-ref to get a list of all the local and tracking branches sorted in descending order by the committer date of the last commit like this:

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

This outputs eg.:

2012-06-23 refs/heads/master 2012-06-21 refs/remotes/origin/HEAD 2012-06-21 refs/remotes/origin/master 

You can add --count=m to get at most m branches, you can --sort=-authordate instead of using the committer date, you can of course use different formats. for-each-ref itself does not limit the result by date, this has to be scripted separately, but at least you have the dates from the commit object in hand.

like image 189
Michał Politowski Avatar answered Oct 03 '22 23:10

Michał Politowski


ls -1 --sort=time .git/refs/heads/ | while read b; do PAGER='' git log -n1 --color --pretty=format:'%C(yellow)%d%Creset - %Cred%h%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' --abbrev-commit $b --; done; 

This oneliner prints all local branches sorted by time from newest to oldest. Each branch has last commit with human readable date string. You can add it to your .gitconfig.

For remote branches I came up with this creepy solution:

git ls-remote -h origin | while read b; do PAGER='' git log -n1 --color --pretty=format:'%ct%C(yellow)%d%Creset - %Cred%h%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' --abbrev-commit $( echo $b | cut -d' ' -f1 ) --; done | sort -rn -k1,10 | cut -c11- 

Edit: the more I think, the more I am afraid this could be unreliable, because ls-remote always connects to remote side whereas log not. It may require to do fetch before every run of this command.

like image 27
jkrcma Avatar answered Oct 03 '22 22:10

jkrcma