Search code examples
gitgit-branch

Print only git branch prefix


I would like to print only the branch name prefixes of a git repo. For example, if a repo had the following branches,

  bug/nasty-problem
  feature/neat-thing
  feature/awesome-thing
  main

is there a way to print only bug, feature, and main?


Solution

  • This will work in your case if you're using terminal etc.

    git branch --list | awk -F'/' '{print $1}' | sort -u
    
    1. git branch --list: Lists all branches in the repository
    2. awk -F'/' '{print $1}': Uses awk to set the field separator to '/' and prints the first field AKA branch name prefix.
    3. sort -u: Sorts the output and removes duplicates (-u stands for unique)