Home Interests Git

How to Display the Current git Branch on the Command Line

howchoo   (467)
August 24, 2023
3 minutes

Share

Interests
Posted in these interests:

Git

git • 5 guides
unix • 2 guides

When you’re using git routinely, it’s helpful to know which branch you’re currently on without having to type git status or git branch.

Fortunately, there’s a convenient way to add your current git branch to your command line prompt. For help with remote branches, check out this guide on how to check out a remote branch.

1 – Open your .bashrc to edit

The .bashrc file contains many of your bash settings. You can open the file to edit using your command line text editor of choice. The file should be located in your home directory.

vim ~/.bashrc

2 – Add the following to your .bashrc

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

You can add this to the end. Then save and close.

3 – Source your .bashrc

If you’re in a folder with an initialized git repo, you can source your bashrc file and see the difference.

source ~/.bashrc
NEXT UP

How to Set up Git Tab Completion

howchoo
howchoo   (467)
November 26, 2023

Tab completion, or auto-completion, is essential if you’re using Git on the command line. Tab completion is a nice feature of many shells that allows you to complete a word by hitting tab. In this case, we want to be able to use tab completion for things like branches and tags in git. Fortunately, setting

Continue Reading

howchoo

 467 guides

Introducing Howchoo, an enigmatic author whose unique pen name reflects their boundless curiosity and limitless creativity. Mysterious and multifaceted, Howchoo has emerged as a captivating storyteller, leaving readers mesmerized by the uncharted realms they craft with their words. With an insatiable appetite for knowledge and a love for exploration, Howchoo's writing transcends conventional genres, blurring the lines between fantasy, science fiction, and the surreal. Their narratives are a kaleidoscope of ideas, weaving together intricate plots, unforgettable characters, and thought-provoking themes that challenge the boundaries of imagination.

Discover interesting things!

Explore Howchoo's most popular interests.

Explore