Order branches in `git branch --all` by branch name

128 Views Asked by At

I have a repository with multiple remotes. When I issue a git branch --all --verbose it shows:

bar                    9876de11 hello world
foo                    12abde23 description
master                 34fd4545 tony the pony
quz                    ab34df67 me, too
remotes/origin/bar     9876de11 hello world
remotes/origin/foo     12abde23 description
remotes/origin/master  34fd4545 tony the pony
remotes/origin/quz     ab34df67 me, too
remotes/zulu/bar       9876de11 hello world
remotes/zulu/foo       12abde23 description
remotes/zulu/master    34fd4545 tony the pony
remotes/zulu/quz       ab34df67 me, too

In this output it's hard to see, if every local branch is on par with its remote counterparts. I'd fancy an output ordered by local branch name:

bar                    9876de11 hello world
remotes/origin/bar     9876de11 hello world
remotes/zulu/bar       9876de11 hello world
foo                    12abde23 description
remotes/origin/foo     12abde23 description
remotes/zulu/foo       12abde23 description
master                 34fd4545 tony the pony
remotes/origin/master  34fd4545 tony the pony
remotes/zulu/master    34fd4545 tony the pony
quz                    ab34df67 me, too
remotes/origin/quz     ab34df67 me, too
remotes/zulu/quz       ab34df67 me, too

This way, skimming over the output to see unpushed changes would be much easier on the eye. A naive solution

git branch -a -v | sort -t / -k 3

doesn't work, because the local entries have no "/" in them for sort to look for.

1

There are 1 best solutions below

2
On BEST ANSWER

This might be a little rough, but try this:

git branch --all --verbose | sed 's/^[ *] //' | while read line; do echo $(basename $(echo $line | awk '{ print $1 }')) $line; done | sort | cut -d' ' -f2-

Basically, we extract the basename of the branch, giving us only the branchname without the remotes/origin/whatever. We then sort by it, then cut it off of the final output via cut. This can be tweaked and cleaned up, but it should give you a starting point. You can also add --color to the initial git branch to preserve the colored output you'd see without piping git to anything.