Sort branches but show current branch first

Can I sort branches by -committerdate, but also always show the current branch first?

git config --global branch.sort -committerdate

Shows more recently committed-on branches ahead of the current branch

I would like

git branch

to show the current branch first.

0

The following should behave pretty much like regular interactive git-branch(1) does.

You will get the abbreviated hash of the current commit if you are not on a branch.

* (HEAD detached at 2dbd1361a0)

And it will be at the top. That’s why we don’t have to handle that case in the output.

#!/bin/sh

# Use colors if we’re being called interactively
if [ -t 1 ];
then
    GREEN='33[0;32m'
    NC='33[0m'
    color_always=--color=always
    less_r=-R
fi

(
    show_current=$(git branch --show-current)
    if [ -n "$show_current" ];
    then
        echo "${GREEN}* $show_current${NC}"
    fi
    git branch $color_always --sort=-committerdate 
        | if [ -n "$show_current" ];
    then
        grep --invert-match --fixed-strings "$show_current"
    else
        cat
    fi
) | less $less_r --quit-if-one-screen

2

On GNU/anything:

git branch --color=always|sed -E '0,/^[^ *]**/{H;//{g;s/(.*)n(.*)/21/p};d}'

so, that line noise:

  • sed -E-Extended regeps
  • 0,/^[ *]**/ — right from the start, up to the first line with a * before the first space,
  • {H;//{…};d} — ignoring the inner braced set for now, H append the incoming line to the internal “hold” buffer, with a leading newline, // rerun the last search, and do the internal set when you get there, d and regardless, drop the incoming line, we’re done with it.

It’s gathering up all the lines up to the one you’re looking for doing something when it finds it, and deleting them. What’s it doing? That above is:

  • g;s/(.*)n(.*)/21/pg get the hold buffer, all the lines so far with n stuck on the front of each, put everything up to the last newline after everything that follows it, and print. Remember H sticking a newline on the front of each line, even the first one in? Yeah, that’s still there.

5

  1. git branch –sort=refname: sort Branches Alphabetically.
  2. grep -E “^*|^”: Filters the list, ensuring the current branch (marked with *) is shown first
    3.sort -k1,1r: Sorts branches, ensuring the current branch is displayed at the top.

You can run directly the below command to sort current branch in first
git branch –sort=refname | grep -E “^*|^” | sort -k1,1r

New contributor

Abinash Mohanty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

jthill’s nice sed answer pointed me in the right direction to implement the same solution but in awk (more portable and more readable for me, I don’t have GNU sed)

git branch --color=always | awk '/^*/ {print} !/^*/ {lines[n++] = $0} END {for ( i = 0; i < n; i++ ) print lines[i]}'

I’m a bit dissatisfied with how long the awk script is for something so simple.

Now I also would like to make this work with columns, like git branch --column.

I can do that just by piping to git column --mode=auto.

git branch –color=always | awk … | git column –mode=auto


This isn’t a perfect answer for me, because it won’t change the default behaviour of git branch. I would like git branch without any arguments to do what this script does.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Sort branches but show current branch first

Can I sort branches by -committerdate, but also always show the current branch first?

git config --global branch.sort -committerdate

Shows more recently committed-on branches ahead of the current branch

I would like

git branch

to show the current branch first.

0

The following should behave pretty much like regular interactive git-branch(1) does.

You will get the abbreviated hash of the current commit if you are not on a branch.

* (HEAD detached at 2dbd1361a0)

And it will be at the top. That’s why we don’t have to handle that case in the output.

#!/bin/sh

# Use colors if we’re being called interactively
if [ -t 1 ];
then
    GREEN='33[0;32m'
    NC='33[0m'
    color_always=--color=always
    less_r=-R
fi

(
    show_current=$(git branch --show-current)
    if [ -n "$show_current" ];
    then
        echo "${GREEN}* $show_current${NC}"
    fi
    git branch $color_always --sort=-committerdate 
        | if [ -n "$show_current" ];
    then
        grep --invert-match --fixed-strings "$show_current"
    else
        cat
    fi
) | less $less_r --quit-if-one-screen

2

On GNU/anything:

git branch --color=always|sed -E '0,/^[^ *]**/{H;//{g;s/(.*)n(.*)/21/p};d}'

so, that line noise:

  • sed -E-Extended regeps
  • 0,/^[ *]**/ — right from the start, up to the first line with a * before the first space,
  • {H;//{…};d} — ignoring the inner braced set for now, H append the incoming line to the internal “hold” buffer, with a leading newline, // rerun the last search, and do the internal set when you get there, d and regardless, drop the incoming line, we’re done with it.

It’s gathering up all the lines up to the one you’re looking for doing something when it finds it, and deleting them. What’s it doing? That above is:

  • g;s/(.*)n(.*)/21/pg get the hold buffer, all the lines so far with n stuck on the front of each, put everything up to the last newline after everything that follows it, and print. Remember H sticking a newline on the front of each line, even the first one in? Yeah, that’s still there.

5

  1. git branch –sort=refname: sort Branches Alphabetically.
  2. grep -E “^*|^”: Filters the list, ensuring the current branch (marked with *) is shown first
    3.sort -k1,1r: Sorts branches, ensuring the current branch is displayed at the top.

You can run directly the below command to sort current branch in first
git branch –sort=refname | grep -E “^*|^” | sort -k1,1r

New contributor

Abinash Mohanty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

jthill’s nice sed answer pointed me in the right direction to implement the same solution but in awk (more portable and more readable for me, I don’t have GNU sed)

git branch --color=always | awk '/^*/ {print} !/^*/ {lines[n++] = $0} END {for ( i = 0; i < n; i++ ) print lines[i]}'

I’m a bit dissatisfied with how long the awk script is for something so simple.

Now I also would like to make this work with columns, like git branch --column.

I can do that just by piping to git column --mode=auto.

git branch –color=always | awk … | git column –mode=auto


This isn’t a perfect answer for me, because it won’t change the default behaviour of git branch. I would like git branch without any arguments to do what this script does.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật