Show git Branch from Command Line

Whether it’s simply submitting pull requests or being snobby enough to use vim as a text editor, web developers and designers spend an awful lot of time working from command line.  If you do work with git, you know it’s important to keep track of your branches, especially when it comes to knowing which branch you’re currently on.

You could frequently execute git branch to ensure you’re on the branch you’d prefer to be on, but that’s a lot of unnecessary repetition.  After all, with the amount of work we do from command line, there should be a way to always display that information…and there is.  Let me show you how to always show the current checked out branch within the command line display!

Start by opening your .bash_profile file — this file is typically used to create command line aliases and set environment variables.  Add the following to the .bash_profile file:

# Slightly modified from: # Show current git branch in command line
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
export PS1="\[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

PS1 represents the leading text display before each line execute in the command line.  With the directive above your command line will display like:

Show Git Branch

Always seeing the current branch name (if any) is a time saver and blanket of security for those of use who use git for our projects.  Do yourself the favor of adding this tiny snippet to your .bash_profile and thank me later!

Are you a Windows user? Check out posh-git to achieve the same functionality!

Related Post

Rojenx is a leading concept artist who work appears in games and publications

Check out his personal gallery here

In other news …