When you check out a branch in a terminal while working on the Git command line, the default command prompt will only display the current working directory information:
-
You can enhance the Git command prompt to display the current branch name and modifications to a branch (indicated by a trailing plus +), by following these steps.
-
Download the following scripts from the Git source code repository located at: https://github.com/git/git/tree/master/contrib/completion
-
git-completion script specific to your terminal. Most users will select git-completion.bash
-
git-prompt.sh
-
-
Place both of the downloaded files into the root of your home directory. On my Mac, that is /Users/maryadmin
-
Copy the following commands into your ~/.bash_profile file (NOTE: replace /home/USERNAME with /Users/<yourusername>)
# Define a variable for the script file export GIT_COMPLETION_PATH="/home/USER_NAME/git-completion.bash" # Display the current branch in the prompt and mark with an '*' if files have been modified since the _checkout_ command. source ~/git-prompt.sh if [ -f "$GIT_COMPLETION_PATH" ]; then GIT_PS1_SHOWDIRTYSTATE=true . "$GIT_COMPLETION_PATH" ADD_PS1='$(__git_ps1)' fi if [[ ${EUID} == 0 ]] ; then PS1="\[\033[01;31m\]\h\[\033[01;34m\] \w\[\033[33m\]$ADD_PS1\[\033[34m\] \$\[\033[00m\] " else PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[33m\]$ADD_PS1\[\033[34m\] \$\[\033[00m\] " fi
-
When you navigate to the repository on your file system, your prompt should now contain the branch name in parenthesis along with a plus symbol to indicate if any files have changed: