How to setup git branch name in bash or shell prompt

Code committed to the wrong branch or merged from wrong branch – and the frustration starts.So how to put Git branch name in your bash prompt for better branch tracking.

No wonder Git is the best VCS tool today to access and manage your code and files in a distributed way. But there are always manual tasks that one needs to do, such as tracking your current working development branch. Coders are often lost in their algorithms and logics that they implement and often forget where they are pushing their code changes or merging changes to/from branches.

  1. Here is a simple solution for tracking your current branch.
  2. Open or connect to your Linux shell terminal.
  3. Use any editor like Nano, Emacs, Gedit or VIM to open bashrc file. Examples below.
    1. $ nano ~/.bashrc      (or)
    2. $ gedit ~/.bashrc      (or)
    3. $ vi ~/.bashrc           (or)
  4. Add following code at the end of your .bashrc file. Save and Exit.

You still may not be able to see the Git branch name in your bash prompt due to the fact that the shell has its old values when you opened/connected to the shell terminal. So if you want to see current branch without logging out and logging again, you can issue following command to load the .bashrc file by your current shell terminal.

$ source ~/.bashrc

The above command is used only when you don’t want to logout and re-login again. By logging out and logging in again will by default instruct shell terminal to load the updated .bashrc file.

That’s it. You will be able to see your branch name in your shell prompt as demonstrated below.

Git Branch Name in Bash Shell Prompt