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?
Late to the party here. The accepted CML answer rocks, but if you want something prettier, like a GUI, and your origin === "github".
You can click "Branches" in the repo. or hit the url direct: https://github.com/ORGANIZATION_NAME/REPO_NAME/branches
Here is a simple command that lists all branches with latest commits:
To order by most recent commit, use
Source: http://git-scm.com/book/en/Git-Branching-Branch-Management
My best result as a script:
I use the following alias:
recent = "!r(){git for-each-ref --sort=-committerdate refs/heads --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)'|column -ts'|'}; r"
which produces:
Edit: use '|' to separate, thanks to @Björn Lindqvist
Update: added * before the current branch, thanks to @elhadi
Edit: fixed a case where the current branch was a substring of another branch
Edit: use a simpler syntax for the current branch, thanks to @Joshua Skrzypek
Here is another script that does what all the other scripts do. In fact, it provides a function for your shell.
Its contribution is that it pulls some colours from your git config (or uses defaults).
Adds some color (since
pretty-format
isn't available)