Show git branch in terminal

When working with git, to avoid accidental commit to wrong branch, it will be better if terminal show the branch name you are currently in.

show git branch in terminal

To display “git branch” in terminal, edit .bashrc

vi ~/.bashrc

Add following code to end of the file.

# Show git branch in command promt if git repo

show_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(show_git_branch)\$ '

You will need to restart terminal after making changes to ~/.bashrc file or run

source ~/.bashrc

See Git

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *