3

I was wondering if anyone had built a script or had a way to list git branches with numbers so that instead of this (preferably in bash)

feature/myusername/ID-1111-my-branch-name
feature/myusername/ID-2222-my-branch-name
feature/myusername/ID-3333-my-branch-name
feature/myusername/ID-4444-my-branch-name

I could get a list like this (or similar)

#1 feature/myusername/ID-1111-my-branch-name
#2 feature/myusername/ID-2222-my-branch-name
#3 feature/myusername/ID-3333-my-branch-name
#4 feature/myusername/ID-4444-my-branch-name

Then checking out a branch would be as easy as

git checkout #4
Hardev
  • 10,851
  • 2
  • 17
  • 17

4 Answers4

7

This sounds like it would be quite easy to create. A simplistic implementation could work with two one-line scripts/aliases.

git-lbr (list branch):

#!/bin/bash

git branch --no-color | cat -n

git-coid (checkout id)

#!/bin/bash

git checkout $(git lbr | egrep "^\s+$1\s+" | egrep -o '\S*$')

But that's a simplistic implementation with no error handling for cases with missing input / non-numeric input / out of range branch ids.

Example of use:

$ git lbr
     1    branch_42
     2    feature/super-feature
     3    foo/bar
     4  * master

$ git coid 1
Switched to branch 'branch_42'
Your branch is up-to-date with 'master'.
Alderath
  • 3,761
  • 1
  • 25
  • 43
1

For Git Bash on Windows, I edited the .gitconfig file in my user directory to have the following section:

[alias]
    lbn = !git branch | cat -n
    cobn = "!f() { branch_name=$(git branch --format='%(refname:short)' | head -n $1 | tail -n 1); git checkout $branch_name; }; f"

This may not be the most sophisticated approach, but it worked for me:

$ git lbn
     1    bugfix/1234_CrashInAboutScreen
     2    feature/feature1
     3    feature/feature2
     4  * master

$ git cobn 2
Switched to branch 'feature/feature1'

Like Alderath's answer, mine is also a simple implementation without input validation or error handling.

Steve Springer
  • 206
  • 2
  • 4
0

The above answer didn't work for me and gave a syntax error :/. Maybe there's a difference between the shells of different OS (I use mac). Here's what I added in my .bashrc file;

# List branches by id
alias gbls='git branch --no-color | cat -n'

# Checkout branch by id
function gcb() {
    local index=$1
    local branches=$(git branch | grep "[^* ]+" -Eo)
    local counter=0

    echo "$branches" | while IFS= read -r branch ; do
        counter=$((counter+1))
        if [ "$index" -eq "$counter" ]; then
            git checkout $branch
        fi
    done
}

Which works like;

enter image description here

Simon Somlai
  • 856
  • 8
  • 16
0

For Powershell, here's a very simple solution (no complex code):

function b {
    $result = git branch;
    $index = 0;
    foreach ($branch in $result)
        {
            Write-Host "    $index. $branch";
            $index = $index + 1;
        }
    $desire = Read-Host -Prompt 'Which branch?'
    $trimmed_desire = $result[$desire].trim();
    git checkout $trimmed_desire;
}

There is no error-checking, though, in this one!

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 08 '22 at 01:19