3

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.

Boldewyn
  • 81,211
  • 44
  • 156
  • 212
  • 1
    Perhaps you could do it with `git for-each-ref`, but it might be tricky; in particular, a remote-tracking branch and its corresponding local branch may not have the same name. – jub0bs Jun 24 '15 at 11:49
  • That's interesting! I've played a bit with it, but haven't come up with a decent solution. However, it looks promising. – Boldewyn Jun 24 '15 at 12:24

1 Answers1

1

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.

Will
  • 24,082
  • 14
  • 97
  • 108
  • Ha! `basename`! That's such a cool idea! I had to tweak it a bit, though: `git branch --all --verbose | sed 's/^[ *] //' | while read line; do echo $(basename $(echo $line | awk '{ print $1 }')) $line; done | sort | cut -d' ' -f2-`. Basically, remove `--color`, since green entries will then be found before red ones, and `sed 's/^[ *] //'` to remove the leading mark for the active branch. – Boldewyn Jun 26 '15 at 09:17
  • 1
    Awesome, thanks :) The --color was included by mistake, I only meant to put it in the explanation below. Great point about removing the current-branch designator. I edited my answer to include your changes. – Will Jun 26 '15 at 09:43