Add Git Branch Name to Bash Prompt

  1. Add the following lines to your ~/.bashrc file:

    parse_git_branch() {
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
    }
    
    export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
    

    Where:

    • \u@\h \[\033[32m\] displays the user, host name, and its color.
    • \w\[\033[33m\] shows the current working directory and its color.
    • \$(parse_git_branch)\[\033[00m\] displays the git branch name and its color.

    Typically, I only display the current working directory and the git branch name without showing the user and hostname.

  2. Run the command source ~/.bashrc to apply the changes.

Source:
Coderwall




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Satu EFI untuk Dua Operating System (Ubuntu 24.04 dan Windows 11) pada dua disk yang berbeda
  • Akses internet ITS melalui myITS-WiFi di Ubuntu
  • Menampilkan Label LaTeX di Matplotlib