While using GIT, isn’t it pretty often that you need to keeping checking what branch you are currently on, or if there is stashed content or not, or if you are ahead/behind of the remote repository and so on ? How about we got this information without having to do a git status
or a git stash show
each time? Git Prompt to the Rescue!
Git prompt helps save precious time by turning your default prompt into a nice colourful one displaying the status of your current git repository. Cutting short, by the end of this article, you will be able turn your regular looking prompt from
TO
nikets-macbook-pro
is the host.niketpathak
is the username.memarena
is the name of the current directory.master
is the name of the branch you are on.*
indicates that you have unstaged changes.%
indicates that you have untracked files.=
indicates that there is no difference between the head and the up-stream. Also,<
would indicate that you are behind,>
would indicate that you are ahead and<>
would mean that you have diverged.+
would indicate that you have staged files.$
would indicate that you have stashed files.
Sweet! right??? Not only do you get to see the current branch you are on, but also the status of the repository (dirty, stashed files, etc.) So, lets dive in to see how we can set this up.
1. Import Git prompt Script to get status of repository
We use a script, git-prompt, provided by the git core team. (I use this forked version for myself. Feel free to use the same). Copy this script to your home directory, ~/.git-prompt.sh
This script is the machinery responsible to get the details and the status of the git repository.
2. Update your bash profile.
Your bash profile is usually located at/etc/bash.bashrc
, /etc/profile
, ~/.bashrc
, ~/.bash_profile
, or ~/.profile
depending upon your OS.
A. Add the following to the end of your bash profile for a colourful prompt (Alternatively, Follow Step B to skip the fancy colours)
# store colors MAGENTA="\[\033[0;35m\]" YELLOW="\[\033[01;33m\]" BLUE="\[\033[00;34m\]" LIGHT_GRAY="\[\033[0;37m\]" CYAN="\[\033[0;36m\]" GREEN="\[\033[00;32m\]" RED="\[\033[0;31m\]" VIOLET='\[\033[01;35m\]' function color_my_prompt { local __user_and_host="$GREEN\u@\h" local __cur_location="$BLUE\W" # capital 'W': current directory, small 'w': full file path local __git_branch_color="$GREEN" local __prompt_tail="$VIOLET$" local __user_input_color="$GREEN" local __git_branch=$(__git_ps1); # colour branch name depending on state if [[ "${__git_branch}" =~ "*" ]]; then # if repository is dirty __git_branch_color="$RED" elif [[ "${__git_branch}" =~ "$" ]]; then # if there is something stashed __git_branch_color="$YELLOW" elif [[ "${__git_branch}" =~ "%" ]]; then # if there are only untracked files __git_branch_color="$LIGHT_GRAY" elif [[ "${__git_branch}" =~ "+" ]]; then # if there are staged files __git_branch_color="$CYAN" fi # Build the PS1 (Prompt String) PS1="$__user_and_host $__cur_location$__git_branch_color$__git_branch $__prompt_tail$__user_input_color " } # configure PROMPT_COMMAND which is executed each time before PS1 export PROMPT_COMMAND=color_my_prompt # if .git-prompt.sh exists, set options and execute it if [ -f ~/.git-prompt.sh ]; then GIT_PS1_SHOWDIRTYSTATE=true GIT_PS1_SHOWSTASHSTATE=true GIT_PS1_SHOWUNTRACKEDFILES=true GIT_PS1_SHOWUPSTREAM="auto" GIT_PS1_HIDE_IF_PWD_IGNORED=true GIT_PS1_SHOWCOLORHINTS=true . ~/.git-prompt.sh fi
This will give you a beautiful prompt along with the branch name that changes its colour depending upon the state of the repository.
B. You can skip the colouring of the prompt and retain the same functionality by adding the following at the end of your bash profile: (Skip this if you did Step A)
# we pass 2 parameters to __git_ps1 as pre and post which are strings. export PROMPT_COMMAND='__git_ps1 "\u@\h:\W" "\\\$ "' # if .git-prompt.sh exists, set options and execute it if [ -f ~/.git-prompt.sh ]; then GIT_PS1_SHOWDIRTYSTATE=true GIT_PS1_SHOWSTASHSTATE=true GIT_PS1_SHOWUNTRACKEDFILES=true GIT_PS1_SHOWUPSTREAM="auto" GIT_PS1_HIDE_IF_PWD_IGNORED=true GIT_PS1_SHOWCOLORHINTS=true . ~/.git-prompt.sh fi
3. Relogin/ Refresh Shell
To see the changes made to your bash profile, you should re-login to bash. You can do this by using
# re-login to bash bash -l
And Viola! You should have a nice Git Prompt showing up in your console (assuming that your current directory is a git repository. If it isn’t, then as expected, the git-prompt will be hidden.)
Resources: My bash profile configuration.
Going further, if you haven’t already, it is recommended to setup a global gitconfig and a global gitignore file to ensure consistencies between all your repositories. Not only that, it helps you being more productive and saves precious keystrokes.
Update Dec 2019 – To take your productivity even further, you might wanna see how to use git hooks to automatically lint your code 😉
References
- http://www.opinionatedprogrammer.com/bash-prompt-reflecting-git-status/
- https://nelsonslog.wordpress.com/2013/04/07/git-prompts-for-bash/
- http://code-worrier.com/blog/git-branch-in-bash-prompt/
- https://rants.broonix.ca/git-status-in-your-bash-prompt/
- https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt
I got this error “bash: __git_ps1: command not found” I am using MacOS 10.12.6 and git 2.16.2
I think you have not placed the
.git-prompt.sh
script at the correct location. Since you’re using a MAC, It is supposed to be at root of your home directory(~
). Ensure this file exists at~/.git-prompt.sh
(see step 1) and re-login into bash viabash -l
.Yes. I too figured it out later. It worked after that. thanks!