Display git branch name in bash prompt

Add to ~/.bash_profile or ~/.bashrc

export EDITOR=vim

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\] $ "

function __build_prompt {

local EXIT="$?" # store current exit code

# define some colors

local RESET='\[\e[0m\]'

local RED='\[\e[0;31m\]'

local GREEN='\[\e[0;32m\]'

local BOLD_GRAY='\[\e[1;30m\]'

# longer list of codes here: https://unix.stackexchange.com/a/124408

# start with an empty PS1

# PS1=""

PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] "

if [[ $EXIT -eq 0 ]]; then

PS1+="${GREEN}√${RESET} " # Add green for success

else

PS1+="${RED}?️️️${EXIT}${RESET} " # Add red if exit code non 0

fi

}

# set the prompt command

# include previous values to maintain Apple Terminal support (window title path and sessions)

# this is explained in /etc/bashrc_Apple_Terminal

PROMPT_COMMAND="__build_prompt${PROMPT_COMMAND:+; $PROMPT_COMMAND}"

link 1 - branch name

link 2 - exit code

Aliaces

open

vim ~/.gitconfig

add

[alias]

lg = log --color --graph --pretty=format:'%Cred%h%Creset%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue)<%an>%Creset' --abbrev-commit

aac = !"git add -A && git commit"

st = status -sb

rs = reset HEAD --hard

cr = config credential.helper store

result:

---

Fixed Alternative especially for Fedora Linux (from the already installed files)

export EDITOR=vim

. /usr/share/git-core/contrib/completion/git-prompt.sh

export GIT_PS1_SHOWDIRTYSTATE=1

export GIT_PS1_SHOWUNTRACKEDFILES=1

export GIT_PS1_SHOWCOLORHINTS=1

export PROMPT_DIRTRIM=2

export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

---

Linux git bash