How can I get a list of Git branches, ordered by m

2019-01-02 21:48发布

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?

24条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-02 22:16

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

查看更多
过分要求
3楼-- · 2019-01-02 22:17

Here is a simple command that lists all branches with latest commits:

git branch -v

To order by most recent commit, use

git branch -v --sort=committerdate

Source: http://git-scm.com/book/en/Git-Branching-Branch-Management

查看更多
未来富二代他爹
4楼-- · 2019-01-02 22:17

My best result as a script:

git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short)|%(committerdate:iso)|%(authorname)' |
    sed 's/refs\/heads\///g' |
    grep -v BACKUP  | 
    while IFS='|' read branch date author
    do 
        printf '%-15s %-30s %s\n' "$branch" "$date" "$author"
    done
查看更多
天屎的翅膀
5楼-- · 2019-01-02 22:18

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: result

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

查看更多
相见恨晚
6楼-- · 2019-01-02 22:18

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).

# Git Branch by Date
# Usage: gbd [ -r ]
gbd() {
    local reset_color=`tput sgr0`
    local subject_color=`tput setaf 4 ; tput bold`
    local author_color=`tput setaf 6`

    local target=refs/heads
    local branch_color=`git config --get-color color.branch.local white`

    if [ "$1" = -r ]
    then
        target=refs/remotes/origin
        branch_color=`git config --get-color color.branch.remote red`
    fi

    git for-each-ref --sort=committerdate $target --format="${branch_color}%(refname:short)${reset_color} ${subject_color}%(subject)${reset_color} ${author_color}- %(authorname) (%(committerdate:relative))${reset_color}"
}
查看更多
霸刀☆藐视天下
7楼-- · 2019-01-02 22:19

Adds some color (since pretty-format isn't available)

[alias]
    branchdate = for-each-ref --sort=-committerdate refs/heads/ --format="%(authordate:short)%09%(objectname:short)%09%1B[0;33m%(refname:short)%1B[m%09"
查看更多
登录 后发表回答