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
—-E
xtended regeps0,/^[ *]**/
— 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/p
—g
get the hold buffer, all the lines so far withn
stuck on the front of each, put everything up to the last newline after everything that follows it, and print. RememberH
sticking a newline on the front of each line, even the first one in? Yeah, that’s still there.
5
- git branch –sort=refname: sort Branches Alphabetically.
- 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
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