Show git branch in bash prompt

Hello everyone!

This is a small guide on how to add the branch name to your bash prompt.

First you’ll need to open up the bash configuration file, usually that’s ~/.bashrc.

Then create the following function inside that file:

function getGitBranch {
  [ -d .git ] && git name-rev --name-only @
}

And then the only thing you need to do is execute that function inside of your bash prompt, this is how I do it:

function getGitBranch {
  [ -d .git ] && git name-rev --name-only @
}

PS1='\[\033[01;34m\]\w \[\033[33m\]$(getGitBranch)\[\033[00m\]>\[\033[00m\] '

And that would look like this inside a git folder:

Terminal screenshot using the git prompt


Last modified on 2023-03-17