How can I display the current branch and folder path in terminal? - macos

I've been watching some of the Team Treehouse videos and they have a very nice looking terminal when working with Git.
For example they have (something similar):
mike#treehouseMac: [/Work/test - feature-branch-name] $ git add .
mike#treehouseMac: [/Work/test - feature-branch-name] $ git commit -m "Some feature."
mike#treehouseMac: [/Work/test - feature-branch-name] $ git checkout master
mike#treehouseMac: [/Work/test - master] $ git status
How can my terminal show me some useful information of what branch I'm on, with colors to distinguish bits of the data I want? Is there some sort of de-facto plugin I haven't found yet?
I'm using Mac OSX 10.8

For anyone looking for how to do this in macOS Catalina or above (10.15+ incl. Big Sur 11.0) which has deprecated bash in favour of zsh, here is my .zshrc file:
parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF='%f'
COLOR_USR='%F{243}'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{39}'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n#%M ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% '
If you don't like the colours I have used, replace the 243/197/39 values with the colour codes as defined here:
https://misc.flogisoft.com/bash/tip_colors_and_formatting

Simple way
Open ~/.bash_profile in your favorite editor and add the following content to the bottom.
Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u#\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "
Add Git Branch To Terminal Prompt (Mac)

It's not about a plugin. It's about prompt tricks in the shell.
For a cool setup in bash, check out the dotfiles project of this guy:
https://github.com/mathiasbynens/dotfiles
To get a fancy prompt, include the .bash_prompt in your ~/.bash_profile or ~/.bashrc.
To get the exact same prompt as in your question, change the export PS1 line at the end of .bash_prompt like this:
export PS1="\[${BOLD}${MAGENTA}\]\u\[$WHITE\]#\[$ORANGE\]\h\[$WHITE\]: [\[$GREEN\]\w\[$WHITE\]\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" - \")\[$PURPLE\]\$(parse_git_branch)\[$WHITE\]] \$ \[$RESET\]"
I ended up using all the .bash* files from this repository about a month ago, and it's been really useful for me.
For Git, there are extra goodies in .gitconfig.
And since you're a mac user, there are even more goodies in .osx.

To expand on the existing great answers, a very simple way to get a great looking terminal is to use the open source Dotfiles project.
https://github.com/mathiasbynens/dotfiles
Installation is dead simple on OSX and Linux. Run the following command in Terminal.
git clone https://github.com/mathiasbynens/dotfiles.git && cd dotfiles && source bootstrap.sh
This is going to:
Git clone the repo.
cd into the folder.
Run the installation bash script.

For Mac Catilina 10.15.5 and later version:
add in your ~/.zshrc file
function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
setopt PROMPT_SUBST
export PROMPT='%F{grey}%n%f %F{cyan}%~%f %F{green}$(parse_git_branch)%f %F{normal}$%f '

My prompt includes:
Exit status of last command (if not 0)
Distinctive changes when root
rsync-style user#host:pathname for copy-paste goodness
Git branch, index, modified, untracked and upstream information
Pretty colours
Example:
To do this, add the following to your ~/.bashrc:
#
# Set the prompt #
#
# Select git info displayed, see /usr/share/git/completion/git-prompt.sh for more
export GIT_PS1_SHOWDIRTYSTATE=1 # '*'=unstaged, '+'=staged
export GIT_PS1_SHOWSTASHSTATE=1 # '$'=stashed
export GIT_PS1_SHOWUNTRACKEDFILES=1 # '%'=untracked
export GIT_PS1_SHOWUPSTREAM="verbose" # 'u='=no difference, 'u+1'=ahead by 1 commit
export GIT_PS1_STATESEPARATOR='' # No space between branch and index status
export GIT_PS1_DESCRIBE_STYLE="describe" # detached HEAD style:
# contains relative to newer annotated tag (v1.6.3.2~35)
# branch relative to newer tag or branch (master~4)
# describe relative to older annotated tag (v1.6.3.1-13-gdd42c2f)
# default exactly eatching tag
# Check if we support colours
__colour_enabled() {
local -i colors=$(tput colors 2>/dev/null)
[[ $? -eq 0 ]] && [[ $colors -gt 2 ]]
}
unset __colourise_prompt && __colour_enabled && __colourise_prompt=1
__set_bash_prompt()
{
local exit="$?" # Save the exit status of the last command
# PS1 is made from $PreGitPS1 + <git-status> + $PostGitPS1
local PreGitPS1="${debian_chroot:+($debian_chroot)}"
local PostGitPS1=""
if [[ $__colourise_prompt ]]; then
export GIT_PS1_SHOWCOLORHINTS=1
# Wrap the colour codes between \[ and \], so that
# bash counts the correct number of characters for line wrapping:
local Red='\[\e[0;31m\]'; local BRed='\[\e[1;31m\]'
local Gre='\[\e[0;32m\]'; local BGre='\[\e[1;32m\]'
local Yel='\[\e[0;33m\]'; local BYel='\[\e[1;33m\]'
local Blu='\[\e[0;34m\]'; local BBlu='\[\e[1;34m\]'
local Mag='\[\e[0;35m\]'; local BMag='\[\e[1;35m\]'
local Cya='\[\e[0;36m\]'; local BCya='\[\e[1;36m\]'
local Whi='\[\e[0;37m\]'; local BWhi='\[\e[1;37m\]'
local None='\[\e[0m\]' # Return to default colour
# No username and bright colour if root
if [[ ${EUID} == 0 ]]; then
PreGitPS1+="$BRed\h "
else
PreGitPS1+="$Red\u#\h$None:"
fi
PreGitPS1+="$Blu\w$None"
else # No colour
# Sets prompt like: ravi#boxy:~/prj/sample_app
unset GIT_PS1_SHOWCOLORHINTS
PreGitPS1="${debian_chroot:+($debian_chroot)}\u#\h:\w"
fi
# Now build the part after git's status
# Highlight non-standard exit codes
if [[ $exit != 0 ]]; then
PostGitPS1="$Red[$exit]"
fi
# Change colour of prompt if root
if [[ ${EUID} == 0 ]]; then
PostGitPS1+="$BRed"'\$ '"$None"
else
PostGitPS1+="$Mag"'\$ '"$None"
fi
# Set PS1 from $PreGitPS1 + <git-status> + $PostGitPS1
__git_ps1 "$PreGitPS1" "$PostGitPS1" '(%s)'
# echo '$PS1='"$PS1" # debug
# defaut Linux Mint 17.2 user prompt:
# PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[01;34m\] \w\[\033[00m\] $(__git_ps1 "(%s)") \$ '
}
# This tells bash to reinterpret PS1 after every command, which we
# need because __git_ps1 will return different text and colors
PROMPT_COMMAND=__set_bash_prompt

In 2019, I think git branch --show-current is a better command than the accepted answer.
$ git branch --show-current
master
(Added in git 2.22 release in June 2019)
It runs much faster as it doesn't need to iterate through all branches. Similarly git branch should be avoided too in the command prompt as it slows down your prompt if you have many local branches.
Put it in a function to use anywhere on command prompt:
# This function returns '' in all below cases:
# - git not installed or command not found
# - not in a git repo
# - in a git repo but not on a branch (HEAD detached)
get_git_current_branch() {
git branch --show-current 2> /dev/null
}
More context:
$ git version
git version 2.23.0

Just Install the oh-my-zsh plugins as described in this link.
It works best on macOS and Linux.
Basic Installation
Oh My Zsh is installed by running one of the following commands in your terminal. You can install this via the command-line with either curl or wget.
via curl
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
via wget
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

for anyone still looking for this , i just installed ohmyz
https://ohmyz.sh/#install and the branches it's showing

In the new Catalina OS for Mac
i) zsh way. Add below lines in .zshrc
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'
}
COLOR_DEF='%f'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{33}'
setopt PROMPT_SUBST
export PROMPT='${COLOR_DIR}%1d${COLOR_DEF}${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '
ii) Or to use old bash, you need to change
System Preference -> Users & Groups -> Right click user user
-> Advanced Option -> Login shell -> /bin/bash
Write .bash_profile as below and restart the system
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'
}
export PS1="\W\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
Output: FolderName BranchName $

The git package installed on your system includes bash files to aid you in creating an informative prompt. To create colors, you will need to insert terminal escape sequences into your prompt. And, the final ingredient is to update your prompt after each command gets executed by using the built-in variable PROMPT_COMMAND.
Edit your ~/.bashrc to include the following, and you should get the prompt in your question, modulo some color differences.
#
# Git provides a bash file to create an informative prompt. This is its standard
# location on Linux. On Mac, you should be able to find it under your Git
# installation. If you are unable to find the file, I have a copy of it on my GitHub.
#
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-git-prompt.sh
#
source /usr/share/git/completion/git-prompt.sh
#
# Next, we need to define some terminal escape sequences for colors. For a fuller
# list of colors, and an example how to use them, see my bash color file on my GitHub
# and my coniguration for colored man pages.
#
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/10-colors.sh
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-less.sh
#
color_start='\e['
color_end='m'
color_reset='\e[0m'
color_bg_blue='44'
#
# To get a fancy git prompt, it's not sufficient to set PS1. Instead, we set PROMPT_COMMAND,
# a built in Bash variable that gets evaluated before each render of the prompt.
#
export PROMPT_COMMAND="PS1=\"\${color_start}\${color_bg_blue}\${color_end}\u#\h [\w\$(__git_ps1 \" - %s\")]\${color_reset}\n\$ \""
#
# If you find that the working directory that appears in the prompt is ofter too long,
# then trim it.
#
export PROMPT_DIRTRIM=3

There are many PS1 generators but ezprompt has the git status (2nd tab 'Status Elements' ) also.

For macOS:
Step 1:
Create and edit a .zshrc file that will be used for terminal configuration.
touch ~/.zshrc; open ~/.zshrc
Step 2:
Add this to your ~/.zshrc file:
function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
setopt PROMPT_SUBST
export PROMPT='%F{grey}%n%f %F{cyan}%~%f %F{green}$(parse_git_branch)%f %F{normal}$%f '

Based on 6LYTH3's answer I've decided to post my own due to some improvements that may come in handy:
Simple solution
Open ~/.bash_profile and add the following content
# \[\e[0m\] resets the color to default color
reset_color='\[\e[0m\]'
# \[\033[33m\] sets the color to yellow
path_color='\[\033[33m\]'
# \e[0;32m\ sets the color to green
git_clean_color='\[\e[0;32m\]'
# \e[0;31m\ sets the color to red
git_dirty_color='\[\e[0;31m\]'
# determines if the git branch you are on is clean or dirty
git_prompt ()
{
# Is this a git directory?
if ! git rev-parse --git-dir > /dev/null 2>&1; then
return 0
fi
# Grab working branch name
git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
# Clean or dirty branch
if git diff --quiet 2>/dev/null >&2; then
git_color="${git_clean_color}"
else
git_color="${git_dirty_color}"
fi
echo " [$git_color$git_branch${reset_color}]"
}
export PS1="${path_color}\w\[\e[0m\]$(git_prompt)\n"
This should:
1) Prompt the path you're in, in color: path_color.
2) Tell you which branch are you.
3) Color the name of the branch based on the status of the branch with git_clean_color
for a clean work directory and git_dirty_color for a dirty one.
4) The brackets should stay in the default color you established in your computer.
5) Puts the prompt in the next line for readability.
You can customize the colors with this list
Sophisticated Solution
Another option is to use Git Bash Prompt, install with this. I used the option via Homebrew on Mac OS X.
git_prompt_list_themes to see the themes but I didn't like any of them.
git_prompt_color_samples to see available colors.
git_prompt_make_custom_theme [<Name of base theme>] to create a new custom theme, this should create a .git-prompt-colors.sh file.
subl ~/.git-prompt-colors.sh to open git-prompt-colors.sh and customize:
The .git-prompt-colors.sh file should look like this with my customization
override_git_prompt_colors() {
GIT_PROMPT_THEME_NAME="Custom"
# Clean or dirty branch
if git diff --quiet 2>/dev/null >&2; then
GIT_PROMPT_BRANCH="${Green}"
else
GIT_PROMPT_BRANCH="${Red}"
fi
}
reload_git_prompt_colors "Custom"
Hope this helps, have a great day!

In Catalina and above, you can open your .zshrc file by running nano ~/.zshrc, and posting the following at the top of the document:
# Show git branch in terminal
function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;220m’
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~
${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '
Exit and save. Restart your terminal, or run exec zsh and that should be it.
OR
You can check out this theme and see if you like it. It is really easy to install and looks good in my opinion:
https://github.com/romkatv/powerlevel10k#configuration
Hope that helps.

From Mac OS Catalina
.bash_profile is replaced with .zprofile
Step 1:
Create a .zprofile
touch .zprofile
Step 2:
nano .zprofile
type below line in this
source ~/.bash_profile
and save(ctrl+o return ctrl+x)
Step 3:
Restart your terminal
To Add Git Branch Name
Now you can add below lines in .bash_profile
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u#\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "
Restart your terminal this will work.
Note:
Even you can rename .bash_profile to .zprofile that also works.

Keep it fast, keep it simple
put this in your ~/.bashrc file.
git_stuff() {
git_branch=$(git branch --show-current 2> /dev/null)
if [[ $git_branch == "" ]];then
echo -e ""
elif [[ $git_branch == *"Nocommit"* ]];then
echo -e "No commits"
else
echo -e "$git_branch"
fi
}
prompt() {
PS1="\e[2m$(date +%H:%M:%S.%3N) \e[4m$(git_stuff)\033[0m\n\w$ "
}
PROMPT_COMMAND=prompt
Then source ~/.bashrc

Did some searching & then adjusted it a bit & settled with this.
vi ~/.zshrc
And within zshrc file ->
function git_branch_name()
{
branch=$(git symbolic-ref HEAD 2> /dev/null | awk 'BEGIN{FS="/"} {print $NF}')
if [[ $branch == "" ]];
then
:
else
echo '- ('$branch')'
fi
}
setopt prompt_subst
prompt='%n %1/ $(git_branch_name) $ '

Related

Show current branch on prompt on zsh shell

I am trying to display the git branch on prompt on Big Sur.
So I have created a script file to run for each new session .zshrc
# Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ [\1]/'
}
export PS1="\u#\h \W\[\033[01;33m\]\$(parse_git_branch)\[\033[00m\] $ "
The above doesn't work it actually displays the string not its outpupt.
\u#\h \W\[\033[01;33m\]$(parse_git_branch)\[\033[00m\] $
How can I show current branch in prompt on zsh shell?
I am assuming you want to display something like this [username#computername directory](branch)
The code below should do it for Zsh which is a little more involved than accomplishing the same task in bash. There are multiple solutions to this, and you can find more information here if you are interested.
Add the following to your .zshrc or .zsh_profile
# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }
# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats '%b'
# Set up the prompt (with git branch name)
setopt PROMPT_SUBST
PROMPT='[%n#%m %1~]%F{green}(${vcs_info_msg_0_})%F{white}$ '
It would be beneficial to read up on the differences between bash and zsh as you cannot use solutions found online interchangeably.
Bash PS1 is not ZSH PS1, they differ. The \u \h etc. sequences are substituted by bash, they do not work in ZSH. Research both shells and how they differ.

macOS terminal configuration problems, setting colours and git branching information

I have a problem with terminal display configuration in macOS Big Sur 11.1
I want it to look like windows terminal from CMDer editor. Which can be used in the picture below
(img) I want to have something like this
I was very used to to this look of a terminal so I started to dig for solution.
I found, that I need to configure .zshrc file to change colour and add git information when needed.
I found the code which needs to be set in .zshrc file:
parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;44m'
COLOR_DIR=$'\e[38;5;106m'
COLOR_GIT=$'\e[38;5;208m'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DEF}in ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}Ⲗ '
And it works when it comes to colours. I’m not able to make parse_git_branch function to work. I don't know what is going on here. In other post there is info that the method works.
(img) currently, I have something like this. With no git branching information
Here you go:
parse_git_branch() {
local branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
# If the branch is non-zero
if [[ -n $branch ]]; then
# -n: Don't print a newline at the end.
# -P: Substitute prompt expansions.
# %F{y}: Foreground color yellow
print -nP - "%F{y}($branch"
local tracking=$(git rev-parse --abbrev-ref --symbolic-full-name #{u} 2>/dev/null)
[[ -n $tracking ]] &&
print -nP - " -> $tracking"
print - ')'
fi
}
setopt promptsubst
# %F{g}: Foreground color green
# %~: pwd with named dir substitution
# %f: Default foreground color
export PS1='%F{g}%~ $(parse_git_branch)
%fⲖ '
References:
How to get the current branch name in Git?
Find out which remote branch a local branch is tracking
Zsh manual: prompt expansion

How does __git_ps1 update the bash prompt?

I'm a Windows user with the Git Bash shell as my daily-driver. I'm curious how the __git_ps1 function updates the prompt every time you change a directory. It's really the only example of updating the bash prompt on the fly that I've seen. I want to leverage this behavior in my own function to add a display on my prompt if I have an RDP session open.
tldr: Any ideas on how the __git_ps1 function evaluates the bash prompt on the fly????
So here is my simple function to see if the RDP client is running
function __rdp_ps1() {
local MATCH=
if tasklist | grep --quiet mstsc; then
MATCH="\e[41mRDP\e[0m"
fi
echo "$MATCH"
}
So the idea is I want to display RDP with a red background, and I want my shell to evaluate this on the fly the same way __git__ps1 is seemingly able to.
What I've investigated (without real success) so far
/etc/profile.d/git-prompt.sh
This block seems to create the PS1 my shell is using
PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
PS1="$PS1"'\n' # new line
PS1="$PS1"'\[\033[32m\]' # change to green
PS1="$PS1"'\u#\h ' # user#host<space>
PS1="$PS1"'\[\033[35m\]' # change to purple
PS1="$PS1"'$MSYSTEM ' # show MSYSTEM
PS1="$PS1"'\[\033[33m\]' # change to brownish yellow
PS1="$PS1"'\w' # current working directory
if test -z "$WINELOADERNOEXEC"
then
GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
if test -f "$COMPLETION_PATH/git-prompt.sh"
then
. "$COMPLETION_PATH/git-completion.bash"
. "$COMPLETION_PATH/git-prompt.sh"
PS1="$PS1"'\[\033[36m\]' # change color to cyan
# tried hamjamming PS1="$PS1 `__rdp_ps1`" here, it only works on login
PS1="$PS1"'`__git_ps1`' # bash function
fi
fi
PS1="$PS1"'\[\033[0m\]' # change color
PS1="$PS1"'\n' # new line
PS1="$PS1"'$ ' # prompt: always $
So I went to see where this file was being sourced to see if that could lead to the answer
/etc/bash.bashrc
Last line held the gold
# Fixup git-bash in non login env
shopt -q login_shell || . /etc/profile.d/git-prompt.sh`
So I evaluated shopt login_shell and it's always on, but I don't really know what that means because the comment leads me to believe that when login env is off, the prompt script will be evaluated
Any ideas???
Your problem might be that you define your $PS1 with double quotes, which bash interprets when executing. Which means that __rdp_ps1 is ran when $PS1 is defined.
In your .bashrc, try replacing the definition with:
PS1='$PS1 `__rdp_ps1`' # Note the single quote.
I have a similar feature on my PS1 (but to display the number of jobs in the background), here is the full version (available here: https://github.com/padawin/dotfiles/blob/master/.bashrc#L70):
function j(){
jobs | wc -l | egrep -v ^0 | sed -r 's/^([0-9]+)/ (\1)/'
}
PROMPT_COMMAND=__prompt_command # Func to gen PS1 after CMDs
__prompt_command() {
local EXIT="$?" # This needs to be first
PS1="$(virtual_env_name)"
local RCol='\[\e[0m\]'
local Red='\e[0;31m'
local Gre='\e[0;32m'
local Blu='\e[1;34m'
PS1+="${Gre}\u#\h$(j)${RCol}: ${Red}\w${Blu}$(__git_ps1)"
if [ $EXIT != 0 ]; then
PS1+="$Red \342\234\226 (${EXIT})"
else
PS1+="$Gre \342\234\224"
fi
PS1+="$RCol\n> "
}
Which can be simplified as the following in .bashrc:
function j(){
jobs | wc -l | egrep -v ^0 | sed -r 's/^([0-9]+)/ (\1)/'
}
PS1='\u$(j) > ' # Note the single quote here
Which behaves as follow:
padawin > vim
[1]+ Stopped vim
padawin (1) > fg
vim
padawin >
Not directly a solution but this might help: if you want to know where the __git_ps1 function lives locally on your filesystem so you can experiment with making edits, you can do
grep -r __git_ps1 /
which searches for the string '__git_ps1' across the file contents of git bash's entire filesystem (where / is actually C:\Program Files\Git or wherever you have it installed).
For me, it was at /mingw64/share/git/completion/git-prompt.sh
My use case was removing the parenthesis from the branch name, which I did by changing this line:
local printf_format=' (%s)'
in the __git_ps1 () function
What you are looking for is PROMPT_COMMAND. Bash will execute whatever is in there before displaying a prompt. If your PS1 is being updated on-the-fly, you probably already have a PROMPT_COMMAND.

bash: git_prompt: command not found

I'm getting this Annoying git_prompt: command not found error. I got a new Mac, and I'm trying to use my old .bash_profile into the new computer. I used Thoughtbot's laptop configuration which worked well last time.
I'm running on Mac Os Sierra, here is my .bash_profile:
# Configuring Our Prompt
# ======================
# if you install git via homebrew, or install the bash autocompletion via homebrew, you get __git_ps1 which you can use in the PS1
# to display the git branch. it's supposedly a bit faster and cleaner than manually parsing through sed. i dont' know if you care
# enough to change it
# This function is called in your prompt to output your active git branch.
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
# This function builds your prompt. It is called below
function prompt {
# Define some local colors
local LIGHT_RED="\[\033[1;31m\]" # really understood
local CHAR="theAsteve :" local BLUE="\[\e[0;49;34m\]"
# ♥ ☆ - Keeping some cool ASCII Characters for reference
# Here is where we actually export the PS1 Variable which stores the text for your prompt
export PS1="theAsteve$ "
PS2='> '
PS4='+ '
}
# Finally call the function and our prompt is all pretty
prompt
export NODE_PATH="/usr/local/lib/node_modules:$NODE_PATH"
export GIT_MERGE_AUTOEDIT='no'
# Editors
# Tells your shell that when a program requires various editors, use sublime.
# The -w flag tells your shell to wait until sublime exits
export VISUAL="vim"
export SVN_EDITOR="vim"
export GIT_EDITOR="vim"
export EDITOR="vim"
# Version
# What version of the Flatiron School bash profile this is
# Paths
# The USR_PATHS variable will just store all relevant /usr paths for easier usage
# Each path is seperate via a : and we always use absolute paths.
# A bit about the /usr directory
# The /usr directory is a convention from linux that creates a common place to put
# files and executables that the entire system needs access too. It tries to be user
# independent, so whichever user is logged in should have permissions to the /usr directory.
# We call that /usr/local. Within /usr/local, there is a bin directory for actually
# storing the binaries (programs) that our system would want.
# Also, Homebrew adopts this convetion so things installed via Homebrew
# get symlinked into /usr/local
export USR_PATHS="/usr/local:/usr/local/bin:/usr/local/sbin:/usr/bin"
# Hint: You can interpolate a variable into a string by using the $VARIABLE notation as below.
# We build our final PATH by combining the variables defined above
# along with any previous values in the PATH variable.
# Our PATH variable is special and very important. Whenever we type a command into our shell,
# it will try to find that command within a directory that is defined in our PATH.
# Read http://blog.seldomatt.com/blog/2012/10/08/bash-and-the-one-true-path/ for more on that.
export PATH="$USR_PATHS:$PATH"
# If you go into your shell and type: echo $PATH you will see the output of your current path.
# For example, mine is:
# /Users/avi/.rvm/gems/ruby-1.9.3-p392/bin:/Users/avi/.rvm/gems/ruby-1.9.3-p392#global/bin:/Users/avi/.rvm/rubies/ruby-1.9.3-p392/bin:/Users/avi/.rvm/bin:/usr/local:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/local/mysql/bin:/usr/local/share/python:/bin:/usr/sbin:/sbin:
# Helpful Functions
# =====================
# A function to CD into the desktop from anywhere
# so you just type desktop.
# HINT: It uses the built in USER variable to know your OS X username
# USE: desktop
# desktop subfolder
function desktop {
cd /Users/$USER/Desktop/$#
}
# A function to easily grep for a matching process
# USE: psg postgres
function psg {
FIRST=`echo $1 | sed -e 's/^\(.\).*/\1/'`
REST=`echo $1 | sed -e 's/^.\(.*\)/\1/'`
ps aux | grep "[$FIRST]$REST"
}
#==================================
# GOLANG PATH
#=================================
export GOPATH=/usr/local/go/bin/go
export PATH=$PATH:$GOPATH/bin
# A function to extract correctly any archive based on extension
# USE: extract imazip.zip
# extract imatar.tar
function extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Bash completion has been installed to:
# /usr/local/etc/bash_completion.d
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
# Case-Insensitive Auto Completion
bind "set completion-ignore-case on"
# Postgres
export PATH=/Applications/Postgres.app/Contents/Versions/9.4/bin:$PATH
if [ -f `brew --prefix`/etc/bash_completion ]; then
. `brew --prefix`/etc/bash_completion
fi
# ===============================================================
# History
# ===============================================================
# Larger bash history
export HISTSIZE=32768
export HISTFILESIZE=$HISTSIZE
# ---------------------
# Colors
# ---------------------
# Adds colors to LS
export CLICOLOR=1
# http://geoff.greer.fm/lscolors/
export LSCOLORS=bxexcxdxbxegedabagacad
# prompt colors
BLACK="\[\e[0;30m\]"
RED="\033[1;31m"
ORANGE="\033[1;33m"
GREEN="\033[1;32m"
PURPLE="\033[1;35m"
WHITE="\033[1;37m"
YELLOW="\[\e[0;33m\]"
CYAN="\[\e[0;36m\]"
BLUE="\[\e[0;34m\]"
BOLD=""
RESET="\033[m"
#----------------------
# style the prompt
# ---------------------
style_user="\[${RESET}${WHITE}\]"
style_path="\[${RESET}${CYAN}\]"
style_chars="\[${RESET}${WHITE}\]"
style_branch="${RED}"
# A more colorful prompt
# \[\e[0m\] resets the color to default color
c_reset='\[\e[0m\]'
# \e[0;31m\ sets the color to red
c_path='\[\e[0;31m\]'
# \e[0;32m\ sets the color to green
c_git_clean='\[\e[0;32m\]'
# \e[0;31m\ sets the color to red
c_git_dirty='\[\e[0;31m\]'
# ---------------------
# Build the prompt
# ---------------------
# Example with committed changes: username ~/documents/GA/wdi on master[+]
__GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion || {
# if not found in /usr/local/etc, try the brew --prefix location
[ -f "$(brew --prefix)/etc/bash_completion.d/git-completion.bash" ] && \
. $(brew --prefix)/etc/bash_completion.d/git-completion.bash
}
source ~/.bash_git
# PS1 is the variable for the prompt you see everytime you hit enter
PS1+="${style_user}\u" # Username
PS1+="${style_path} \w" # Working directory
PS1+="\$(prompt_git)" # Git details
PS1+="\n" # Newline
PS1+="${style_chars}\$ \[${RESET}\]" # $ (and reset color)
export PS1='\t H#\! \u:\w$(__git_ps1 "{%s}") -->> '
I tried some of the other posts regarding the same issue. I tried going over the following code without success. -bash: __git_ps1: command not found
At
I've used this feature for years and this works when adding to the bottom of ~/.bashrc
Setup & Config source: https://github.com/jimeh/git-aware-prompt
function parse_git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"
PS1="$GREEN\u#\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "
Here's what it looks like:
user#server:~/dev/project (Some-Branch-Feature)$
You will have to toy around with it to your liking but hope it gets you somewhere.

Something in my bashrc file causing bash: command not found?

When I open up a new shell I get:
Last login: Sun Mar 23 10:14:46 on ttys000
-bash:  : command not found
I'm not totally sure how to figure out what's going on there, as its not totally clear which command its talking about.
Is it likely something in the .bashrc file?
HISTSIZE=10000
HISTFILESIZE=20000
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
export PS1="\[\e[01;32m\]\h \[\e[01;34m\]\W \$(parse_git_branch)\[\e[01;34m\]$\[\e[00m\] "
export PYTHONSTARTUP=/Users/JimShook/.pythonstartup
export WORKON_HOME=$HOME/.virtualenv
source /usr/local/bin/virtualenvwrapper.sh
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
PATH=$PATH:/usr/local/rvm/bin # Add RVM to PATH for scripting
### Added by the Heroku Toolbelt
Or maybe the bash_profile?
if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi
[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm" # Load RVM into a shell session *as a function*
Any thoughts are helpful.
Things to check - as suggested above:
anything changed recently? if so, start there... (added new line/command/sourced file?)
look for 'control codes' in the your file(s)
Try:
cat -v ~/.bashrc ## look for '^M' or other special chars - remove if present
bash -n ~/.bashrc ## try the 'sourced files' separately
If using VI, some thing to try after opening the file:
:syntax off ## turn off colors - may be easier to see 'hidden' codes
:set list ## control codes may be visible

Resources