When in a repository directory, show the name of the currently checked out Git branch in the prompt.
How to get this working:
First you need to get
git-branch.bash
script (see it here) and put it in your home directory:Notes: Explanation of the above file you just downloaded.
In the above file which you copied at location: ~/.git-branch.bash. we have declared a new environment variable PS1. Please read following notes to know what to do in this case.
Depending on configuration changes you may have made previously you may already have aPS1
variable being exported. In this case you will need to add\$(parse_git_branch)
somewhere in the existing value.
The\[\033[32m\]
parts set the color of the branch text. If you prefer another color check online for a reference on valid values.
The\
in\$(parse_git_branch)
is important to ensure the function is called each time the prompt is displayed; without it, the displayed branch name would not be updated when, for example, checking out a different branch or moving in and out of a Git repository directory.
Open the Terminal app and if the file
~/.bash_profile
does not already exist create it with the following command.Next, add the following lines to your .bash_profile. This tells the bash to execute the git branch script if it exists.
Note: You can use the file~/.bashrc
instead to make this method applicable to Linux.
Now when you change to a directory that is within a GIT repository, the name of the current branch will be added to the prompt of the terminal. This branch name in the prompt will update automatically when you switch branches.
If you are using an existing Terminal session, don’t forget to make the changes take effect by sourcing the file with the command: