Getting error: bash: parse_git_branch: command not found - bash

I am running the command:
sudo bash
But I keep getting an error on my terminal that says,
bash: parse_git_branch: command not found
Here is my .bash_profile file
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\] $ "
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM$
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
export PATH=/usr/local/bin:/Applications/XAMPP/xamppfiles/bin:$PATH
export PATH=/bin:/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH
export EDITOR='subl -w'
export JAVA_HOME=$(/usr/libexec/java_home)
export JAVA_HOME=$(/usr/libexec/java_home)
export JAVA_HOME=$(/usr/libexec/java_home)
Thanks in advance.

The problem is that parse_git_branch is defined in .bash_profile, but not exported. When you run sudo bash, it starts an nonlogin shell that sources .bashrc instead of .bash_profile. PS1 was exported and so is defined in the new shell, but parse_git_branch is not.
Typically, you would define both PS1 and parse_git_branch in .bashrc and export neither of them. macOS is a little different from Linux, in that a terminal emulator starts a login shell instead of an ordinary interactive shell. A good practice is to put the definitions in .bashrc, then source .bashrc from .bash_profile.
Here's how I would split up your existing .bash_profile:
In .bashrc:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
PS1="\u#\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM$
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
In .bash_profile:
# Many of the paths you were adding to PATH should already be
# there in the default configuration; run /usr/lib/path_helper
# to see the default.
PATH=/Applications/XAMPP/xamppfiles/bin:/usr/local/sbin:$PATH
export EDITOR='subl -w'
export JAVA_HOME=$(/usr/libexec/java_home)
[[ -f ~/.bashrc ]] && source ~/.bashrc

What fixed the issue in my case was adding the hashbang (#!/bin/bash - or could be tcsh in the end in your case) to the beginning of my .bashrc.
Without hashbang at the top of .bashrc the function still applied to PS1 but only within the main partition (on my SSD), but when I tried to manipulate git repositories on another mount (an extra HDD) I had this message.
So just adding #!/bin/bash and then reloading .bashrc (by doing source ~.bashrc) fixed this issue for me.

By looking at your .bash_profile, it seems you have forgotten to add the keyword function before parse_git_branch() {}
Try changing to,
function parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
After that, reload your .bash_profile and see if it works.
Reference: Functions in Bash.

Related

macOS shell: JAVA_HOME is empty inside script

I have macOS 10.11.6. The JAVA_HOME is set in the .bash_profile as
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export PATH=$JAVA_HOME/bin:$PATH
From command line, I can see it
>echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home
But, the JAVA_HOME is empty, when I run the following script
#!/bin/sh
SHELL=/bin/sh
export SHELL
echo $JAVA_HOME
if [ -n "$JAVA_HOME" ]; then
#do something
else
echo JAVA_HOME is empty
fi
Even if I use #!/bin/bash in my script, the JAVA_HOME is still empty.
TL;DR
Depending on how you run your scripts, you may need to put your exports into ~/.bashrc for interative shells, or directly into your script or crontab so that it doesn't rely on exports from your user's shell.
Bash Startup Files
Simplistically, the ~/.bash_profile script is sourced for login shells (e.g. /bin/bash -l), while the ~/.bashrc file is sourced for interactive shell (e.g. /bin/bash -i). It's actually a bit more complicated than that, but the above should get you pointed in the right direction.
See Also
https://www.gnu.org/software/bash/manual/html_node/Invoking-Bash.html#Invoking-Bash
https://www.freebsd.org/cgi/man.cgi?query=bash#INVOCATION
man 1 bash
I configure mine like this:
# configure Java http://www.jenv.be/
# install Java: brew cask install java, brew cask install java7
# set global default: setJavaGlobal 1.7, jenv global 1.7
# set local folder default: jenv local 1.8
#
JENV_HOME=$HOME/.jenv
export PATH=.:$PATH:$JENV_HOME/bin
eval "$(jenv init -)"
export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"
alias jenv_set_java_home='export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"'
And some related shell methods I use in my .bash_profile :
setJavaGlobal() {
jenv global $1;
jenv_set_java_home
echoJavaSetup
}
setJavaLocal() {
jenv local $1;
jenv_set_java_home
echoJavaSetup
}
echoJavaSetup() {
echo --------------------
echo NEW JAVA SETUP:
echo " PATH: $PATH"
export JAVA_VERSION=`java -version 2>&1 >/dev/null | grep 'java version' | awk '{print $3}'`
echo " JAVA: $JAVA_VERSION, $JAVA_HOME"
jenv versions
echo --------------------
}
removeJavaLocal() {
rm -rf ./.java-version
}
showJava() {
echo --------------------
echo EXISTING JAVA SETUP:
echo " PATH: $PATH"
export JAVA_VERSION=`java -version 2>&1 >/dev/null | grep 'java version' | awk '{print $3}'`
echo " JAVA: $JAVA_VERSION, $JAVA_HOME"
jenv versions
if [ -f ./.java-version ]; then
echo "Using Java LOCAL DEFAULT. Not using global default! Run command 'removeJavaLocal' to change to global default."
fi
echo --------------------
}

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

Why am I getting "-bash: 'alias: command not found", but it's there..?

I dont understand why my alias doesnt work, any idea people? I want to use 'pro' as an alias
thanks! ^_^)
this is how my bash-file looks like:
export PATH="/usr/local/bin:$PATH"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
# Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u#\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
#******ALIAS FOR TEALEAF_COURSE3**********************
alias pro='cd /Users/yoniPacheko/Documents/ruby/RnR/third_course_0/projects '
well I was looking all the time for the .bash_profile but I've zsh as a shell, so I was looking at the wrong place.
The right place is .zshrc, for those that don't run a bash-shell and want to make aliases
But thanks for yr inputs community, I got the picture finally.
:)

Git branch name in prompt

I am able to show the git branch name in the shell prompt. But whenever I am using screen I am getting
bash: parse_git_branch: command not found
and git branch is not shown.
Please help me get this in the screen sessions also.
I have following in my .bash_profile.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/
}
export PS1="[\W\$(parse_git_branch)]$ "
I don't have .git-completion.bash
System specs:
OS: OSX 10.8.4
Terminal & iTerm2
Screen version: 4.00.03 (FAU) 23-Oct-06
I had the same issue when running under screen and was able to resolve by moving the definition of the parse_git_branch() function from .bash_profile to .bashrc.
When you open your terminal, .bash_profile is executed and therefore PS1 is defined. Then you execute screen, and screen reads the environment variable PS1 which includes a call to parse_git_branch and tries to parse it. But, since screen didn't execute .bash_profile the function parse_git_branch is not defined inside screen.
Move the definition of PS1 to .bashrc because both, screen and iTerm execute it.
This is much simpler and avoids the unnecessary sed:
parse_git_branch () {
while read -r branch; do
[[ $branch = \** ]] && current_branch=${branch#* }
done < <(git branch 2>/dev/null)
[[ $current_branch ]] && printf ' [%s]' "$current_branch"
}
You are missing a ' at the end of your sed statement:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="[\W\$(parse_git_branch)]$ "
Othewerise, it seems to work for me in bash-3.2
I had the same error in OS X High Sierra when switching to root or when starting to ssh-agent /bin/bash I resolved it to put it in /etc/bashrc with check if i am root
if [[ $UID == 0 ]]; then
PS1="\[\e[1;31;40m\]\u#\h \W\[\e[0m\]\$ "
else
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
PS1="\u#\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
fi

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

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) $ '

Resources