You can get a list of all the branches in git using the very programmatically named
for-each-ref command. This command by itself isn’t very useful and needs quite a bit of tailoring for it to produce the information you want.
Just by itself it returns all refs
# returns all the references
This shows all refs (branches, tags, stashes, remote branches). To just get branches pass an argument that corresponds to the
.git directory that contain branch refs.
git for-each-ref ref/heads
To reduce the number of refs it produces use
git for-each-ref --count=5
# returns just 5 refs
This outputs a line that looks like this:
c1973f1ae3e707668b500b0f6171db0a7c464877 commit refs/heads/feature/some-feature
Which is a bit noisy. To reduce the noise we can use
git for-each-ref --format="$(refname)"
# line is just: refs/heads/feature/some-feature
Which can be shortened with
git for-each-ref --format="$(refname:short)"
To get most recent branches we’ll need to sort. The
- in front of the field name reverses the sort.
git for-each-ref --sort=-committerdate
git for-each-ref \