Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.4k points)

I want to get a list of all the branches in a Git repository with the "freshest" branches at the top, where the "freshest" branch is the one that's been committed to most recently (and is, therefore, more likely to be one I want to pay attention to).

Is there a way I can use Git to either (a) sort the list of branches by latest commit, or (b) get a list of branches together with each one's last-commit date, in some kind of machine-readable format?

Worst case, I could always run git branch to get a list of all the branches, parse its output, and then git log -n 1 branchname --format=format:%ci for each one, to get each branch's commit date. But this will run on a Windows box where spinning up a new process is relatively expensive, so launching the Git executable once per branch could get slow if there are a lot of branches. Is there a way to do all this with a single command?

1 Answer

0 votes
by (27.5k points)

Use the following command: 

$ git branch --sort=-committerdate

Alternatively, use the following command to do the same:

$ git config branch.sort -committerdate

Whenever you list branches in the current repository, it will be a sorted list based on the 'committerdate'.

In other cases, if you list branches and want them sorted by comitterdate:

$ git config --global branch.sort -committerdate

Browse Categories

...