Git for Windows doesn't execute my .bashrc file - windows

I've just installed Git for Windows 2.5.0 on Windows 7, and it appears that my .bashrc file is not being executed when I run Git Bash.
I created the file like so:
Administrator#HintTech-Dev MINGW64 /
$ pwd
/
Administrator#HintTech-Dev MINGW64 /
$ cd ~
Administrator#HintTech-Dev MINGW64 ~
$ pwd
/c/Users/Administrator
Administrator#HintTech-Dev MINGW64 ~
$ touch .bashrc
Administrator#HintTech-Dev MINGW64 ~
$ vi .bashrc
[... I insert the line "ZZZTESTVAR=234" (without the quotes) into the file in vim ...]
Administrator#HintTech-Dev MINGW64 ~
$ exit
Yet, when I next run Git Bash:
Administrator#HintTech-Dev MINGW64 /
$ set | grep ZZZ
Administrator#HintTech-Dev MINGW64 /
$ cat ~/.bashrc
ZZZTESTVAR=234
Administrator#HintTech-Dev MINGW64 /
$ ZZZTESTVAR=234
Administrator#HintTech-Dev MINGW64 /
$ set | grep ZZZ
ZZZTESTVAR=234
Administrator#HintTech-Dev MINGW64 /
$
Why isn't my .bashrc being run? It seems to be in the right place and have the right permissions.

OK, I found out the problem. Quite simply, the bash terminal used by the latest Git for Windows 2.5.0 (mintty) doesn't bother to read .bashrc - it reads .bash_profile. So you can set up your environment in .bash_profile and/or put this code at the start to read .bashrc:
if [ -f ~/.bashrc ]
then
. ~/.bashrc
fi

Same thing happened to me when I upgraded to Git Bash 2.5.0 in Windows 10. I renamed my '.bashrc' -> '.bash_profile' and relaunched Git Bash. Everything's working as usual again.
mv ~/.bashrc ~/.bash_profile

It appears the latest version of git for Windows (2.8.3.windows.1) uses a 'profile' file now instead of the .bash_profile. I assume this is so it isn't hidden and a valid file name. Didn't seem to cause any issues before, but maybe it was confusing to people.

A bit late on this answer perhaps, but you could call bash with the -rcfile argument followed by the location of your .bashrc file.
bash -rcfile C:\Users\name\.bashrc
I've added this to the end of my PowerShell profile (.ps1 file) so that Powershell automatically opens in bash and configured to my preferences.

Related

zsh is not Recognizing an Environmental Variable - No such File or Directory - Unix - MacOs

Expected behavior
List the documents in a directory using an environmental variable.
Steps to Reproduce
INPUT
$ export ICLOUD_D="~/Library/Mobile\ Documents/com~apple~CloudDocs/"
$ source ~/.zshrc
$ echo $ICLOUD_D
$ ls $ICLOUD_D
OUTPUT
$ ~/Library/Mobile\ Documents/com~apple~CloudDocs/
$ ls: ~/Library/Mobile\ Documents/com~apple~CloudDocs/: No such file or directory
Current behavior
Shell gives me a: No such file or directory error.
Steps Performed Thus Far to Fix
If I were to simply cut and paste the file path with the command ls then I'm able to list the files as expected.
I've also tried to put quotes around the environmental variable as well.
$ ls "$ICLOUD_D"
System information
- OS: MacOS v 10.15
- Shell: zsh
Do not put quotes around the environmental variable
INPUT
$ export ICLOUD_D=~/Library/Mobile\ Documents/com~apple~CloudDocs/
$ source ~/.zshrc
$ echo $ICLOUD_D
$ ls $ICLOUD_D
OUTPUT
$ ~/Library/Mobile\ Documents/com~apple~CloudDocs/
$ afile
$ bfile
$ ...

Uninstalled Anaconda still shows up in PATH (Mac OS X)

I have installed Anaconda a few months ago but then uninstalled it and removed all anaconda files by using
rm -rf ~/anaconda
but when I run
echo $PATH
it still outputs a path that point to an Anaconda folder but when I search for it, it doesn't even exist, why is that happening?
What makes you think that non-existent directory are automatically
removed from $PATH? They are not. As an example I can make a new dir
and go there:
$ mkdir /tmp/new-path-dir && cd /tmp/new-path-dir
Add it to the $PATH:
$ PATH=/tmp/new-path-dir:$PATH
$ echo $PATH
/tmp/new-path-dir:<REST_OF_PATH>
Make a new olleh.so (hello spelled backwards) executable inside
it:
$ echo 'echo hi' > olleh.so && chmod +x olleh.so
Then go back to ~:
$ cd ~
And start a olleh.so:
$ olleh.so
hi
Now I can safely remove /tmp/new-path-dir:
$ rm -r /tmp/new-path-dir/
And it still will be shown in my $PATH:
$ echo $PATH
/tmp/new-path-dir:<REST_OF_PATH>
But I won't be able to run olleh.so any more:
$ olleh.so
bash: /tmp/new-path-dir/olleh.so: No such file or directory
And as paths to executables are cached by bash I can get rid of
olleh.so permanently like this:
$ hash -r
$ olleh.so
bash: olleh.so: command not found

Unknown error in Bash [duplicate]

I'm trying to change my command promt in terminal. I keep getting the error:
-bash: __git_ps1: command not found
I've tried it just by typing it into the terminal as is: __git_ps1. I've also tried it out in the .bash_profile
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
As you might be able to see/tell, yes, I do have the auto-completion installed and it does work great!
I came across this question: " PS1 env variable does not work on mac " which gives the code
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"
So I add it to my .bash_profile hoping that it will change something. Well, it did. It just changed the error output.
Here's the .bash_profile with the addition:
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
And now here's the changed error output:
sed: (%s): No such file or directory
Note: I've also moved the alias below the source with no difference. I have git version 1.7.12.1
This should be a simple change. Can someone please help me?
Edit 10/13/12
No, I definitely do not want to define __git_ps1 myself but was just trying to see if it would be recognized by doing so. Yes, I have the .git-completion.bash file installed. Here's how I got auto completion on my machine.
cd ~
curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash
mv ~/git.completion.bash ~/.git-completion.bash
A ls -la then lists the .git-completion.bash file.
Edit 10/13/12 - Solved by Mark Longair (below)
The following code worked for me in the .bash_profile while others did not...
if [ -f ~/.git-prompt.sh ]; then
source ~/.git-prompt.sh
export PS1='Geoff[\W]$(__git_ps1 "(%s)"): '
fi
You've installed the version of git-completion.bash from master - in git's development history this is after a commit that split out the __git_ps1 function from the completion functionality into a new file (git-prompt.sh). The commit that introduced this change, which explains the rationale, is af31a456.
I would still suggest that you just source the version of git-completion.bash (or git-prompt.sh) that is bundled with your installation of git.
However, if for some reason you still want to use this functionality by using scripts separately downloaded from master, you should download git-prompt.sh similarly:
curl -o ~/.git-prompt.sh \
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
... and add the following line to your ~/.bash_profile:
source ~/.git-prompt.sh
Then your PS1 variable that includes __git_ps1 '%s' should work fine.
After upgrading to OSX 10.9 Mavericks I had to reference the following files to get git shell command completion and git prompt to work again.
From my .bash_profile or similar:
if [ -f /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash ]; then
. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
fi
source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
#shell prompt example
PS1='\u $(__git_ps1 "(%s)")\$ '
You should
$ brew install bash bash-completion git
Then source "$(brew --prefix)/etc/bash_completion" in your .bashrc.
Following worked for me like a charm:
Run following in your Terminal:
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
Open/Create bash_profile:
$ vi ~/.bash_profile
Add following to the file:
source ~/.bash_git
export PS1='\[\033[01;32m\]os \[\033[01;34m\]\w $(__git_ps1 "[%s]")\$\[\033[00m\] '
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUPSTREAM="auto"
Finally, source it using:
$ source ~/.bash_profile
This will solve the problem of bash: __git_ps1: command not found.
Also your prompt will change to "os ". To change "os" to something else, modify "os" string in export PS1 line.
Solution for MacOS Sierra and git version 2.10.1 <2017-2-06>
Step 1: Install the Git
You can skip this step if you already installed the latest git.
Download git package from browser https://git-scm.com/download/
Note: if you install with curl [option] https://... option to download, you would have to make sure your system support SSL. So for new comer, to download from browser and install directly from git installer is much easier.
Installation Verification:
Show where is your git directory at: which git
Show which version your git currently is: git --version current version should be 2.10.1.
Step 2: Add your git profile to your shell
Open your shell profile:
nano ~/.bash_profile or nano ~/.bashrc Depends on where your modification is.
Add the following code to the file:
source /usr/local/git/contrib/completion/git-completion.bash
source /usr/local/git/contrib/completion/git-prompt.sh
Note: git installation location changed from opt/ directory to usr/local/ after OSX upgrade to El Capitain, and this is why some of the old answer above doesn't work anymore in MacOS Sierra.
Add the following code to your PS1 configuration:
Option 1: add directly to your PS1: export PS1='\w$(__git_ps1 "(%s)") > '
I prefer this simple approach since I already know the .git-completion.bash is there in my home directory, and I can add other prompt format in the front of it. here is my personal prompt for your reference: export PS1='\t H#\! \u:\w$(__git_ps1 "{%s}") -->> '
Option 2: Add a selection script
if [ -f ~/.git-completion.bash ]; then
export PS1='\w$(__git_ps1 "(%s)") > '
fi
Save and use the profile: source ~/.bash_profile or source ~/.bashrc
Now you should see the git prompt working properly and shows which branch you are in right now.
High Sierra clean solution with colors !
No downloads. No brew. No Xcode
Just add it to your ~/.bashrc or ~/.bash_profile
export CLICOLOR=1
[ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh ] && . /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
export GIT_PS1_SHOWCOLORHINTS=1
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUPSTREAM="auto"
PROMPT_COMMAND='__git_ps1 "\h:\W \u" "\\\$ "'
I had same problem when upgrading to Yosemite.
I just had to modify ~/.bashrc to source /usr/local/etc/bash_completion.d/git-prompt.sh instead of the old path.
then re-source your . ~/.bashrc to get the effect.
__git_ps1 for bash is now found in git-prompt.sh in /usr/local/etc/bash_completion.d on my brew installed git version 1.8.1.5
this works in OS 10.8 in the .bash_profile
if [ -f ~/.git-prompt.sh ]; then
source ~/.git-prompt.sh
export PS1='YOURNAME[\W]$(__git_ps1 "(%s)"): '
fi
For macports I had to add: source /opt/local/share/git-core/git-prompt.sh to my ./profile
If you're hoping to use Homebrew to upgrade Git and you've let your system become out-of-date in general (as I did), you may need to bring Homebrew itself up-to-date first (as per brew update: The following untracked working tree files would be overwritten by merge: thanks #chris-frisina)
First bring Homebrew into line with the current version
cd /usr/local
git fetch origin
git reset --hard origin/master
Then update Git:
brew upgrade git
Problem Solved! ;-)
At least with Xcode 6, you already have git-completion.bash. It's inside the Xcode app bundle.
Just add this to your .bashrc:
source `xcode-select -p`/usr/share/git-core/git-completion.bash
Download the files git-prompt.sh and git-completion.bash from this Git completion
Rename the files.
Move those files to your home directory.
Add the source file in to the .bash_profile
source ~/git-completion0.bash
source ~/git-prompt0.sh
and four to trigger the code block.
I know it's not a real answer...
I had some strange issues with sourcing git-prompt.sh in my .bashrc so I started to look for other solution. This one: http://www.jqno.nl/post/2012/04/02/howto-display-the-current-git-branch-in-your-prompt/ doesn't use __git_ps1 and author claims it works also on Mac (for now it works perfectly on my Ubuntu and it's easy to tweak).
I hope it helps!
I was doing the course on Udacity for git hub and was having this same issue. Here is my final code that make is work correctly.
# Change command prompt
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \ . (.*\)/(\1)/'"
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
source ~/.git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff
# '\W' adds the name of the current directory
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"
It works!
https://i.stack.imgur.com/d0lvb.jpg
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh -o ~/.git-prompt.bash
[[ -f ~/.git-prompt.bash ]] && . ~/.git-prompt.bash
# Available GIT_PS1 options/env vars
cat ~/.git-prompt.bash | grep GIT_PS1_ | sed -r 's,^\s*#.*,,' | grep -v -E '^$' | sed -r 's,^.*(GIT_PS1_[A-Z_]+).*,\1,' | sort | uniq | sed -r 's,^(.*)$,export \1=,'
export GIT_PS1_COMPRESSSPARSESTATE=
export GIT_PS1_DESCRIBE_STYLE=
export GIT_PS1_HIDE_IF_PWD_IGNORED=
export GIT_PS1_OMITSPARSESTATE=
export GIT_PS1_SHOWCOLORHINTS=
export GIT_PS1_SHOWDIRTYSTATE=
export GIT_PS1_SHOWSTASHSTATE=
export GIT_PS1_SHOWUNTRACKEDFILES=
export GIT_PS1_SHOWUPSTREAM=
export GIT_PS1_STATESEPARATOR=
for i in $(cat ~/.git-prompt.bash | grep GIT_PS1_ | sed -r 's,^\s*#.*,,' | grep -v -E '^$' | sed -r 's,^.*(GIT_PS1_[A-Z_]+).*,\1,' | sort | uniq); do varname=$i; declare -g ${i}=1; done
# P.S Above is just illustration not all config vars are [0/1].
# For more info:
cat ~/.git-prompt.bash | sed -r -n -e '1,/^[^\s*#]/p' | head -n -2
This one worked for me, and it has colored git output and an indicator in the prompt whether files have changed / been added, right baked into it:
GIT_PS1_SHOWDIRTYSTATE=true
. /usr/local/Cellar/git/1.8.5.2/etc/bash_completion.d/git-completion.bash
. /usr/local/Cellar/git/1.8.5.2/etc/bash_completion.d/git-prompt.sh
PS1='\[\033[32m\]\u#\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '
Be sure to use the correct path! I used homebrew to install git, use brew list git to get the path to your current installation.
Would be nice not to use a hard coded path, but don't know how to get the path to the current installation.
More infos here: http://en.newinstance.it/2010/05/23/git-autocompletion-and-enhanced-bash-prompt/
For git, there are /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh. And please look /etc/bashrc_Apple_Terminal too.
So, I put these in ~/.bash_profile:
if [ -f /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh ]; then
. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
export GIT_PS1_SHOWCOLORHINTS=1
export GIT_PS1_SHOWDIRTYSTATE=1
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }__git_ps1 '\u:\w' '\\\$ '"
fi
Yet another option I just installed on Mojave: magicmonty/bash-git-prompt
Run (brew update) and brew install bash-git-prompt or brew install --HEAD bash-git-prompt
Then to your ~/.bash_profile or ~/.bashrc:
if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
__GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
GIT_PROMPT_ONLY_IN_REPO=1
source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi
I'm happy.
Please not that, if you haven't installed git through Xcode or home-brew, you'll likely find the bash scripts haysclarks refers to in /Library/Developer/CommandLineTools/, and not in /Applications/Xcode.app/Contents/Developer/, thus making the lines to include within .bashrc the following:
if [ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash ]; then
. /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
fi
source /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
You'll need those lines if you wish to use git-prompt as well.
[1]: https://stackoverflow.com/a/20211241/4795986
Copy/Download the following files and copy them to home directory: ~/
git-completion.bash
git-prompt.sh
For the bash_profile, add this at the beginning:
source ~/git-completion.bash
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
For more and easy downloads you could check this.

Mac terminal keeps giving message

Whenever I open my terminal on my mac I get this message, -bash: /usr/local/bin: is a directory
How do I remove this? As I find it annoying and unneeded.
There is probably a line trying to execute /usr/local/bin (which is a directory and not a executable) in either your ~/.bash_profile or your ~/.bashrc
If you want to view the contents of these files (as you asked above) you can type in your terminal after you open it:
cat ~/.bash_profile
and
cat ~/.bashrc
You can try to verify what lines mention /usr/local/bin by typing these commands:
cat ~/.bash_profile | grep "/usr/local/bin"
and
cat ~/.bashrc | grep "/usr/local/bin"

(Mac) -bash: __git_ps1: command not found

I'm trying to change my command promt in terminal. I keep getting the error:
-bash: __git_ps1: command not found
I've tried it just by typing it into the terminal as is: __git_ps1. I've also tried it out in the .bash_profile
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
As you might be able to see/tell, yes, I do have the auto-completion installed and it does work great!
I came across this question: " PS1 env variable does not work on mac " which gives the code
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"
So I add it to my .bash_profile hoping that it will change something. Well, it did. It just changed the error output.
Here's the .bash_profile with the addition:
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
And now here's the changed error output:
sed: (%s): No such file or directory
Note: I've also moved the alias below the source with no difference. I have git version 1.7.12.1
This should be a simple change. Can someone please help me?
Edit 10/13/12
No, I definitely do not want to define __git_ps1 myself but was just trying to see if it would be recognized by doing so. Yes, I have the .git-completion.bash file installed. Here's how I got auto completion on my machine.
cd ~
curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash
mv ~/git.completion.bash ~/.git-completion.bash
A ls -la then lists the .git-completion.bash file.
Edit 10/13/12 - Solved by Mark Longair (below)
The following code worked for me in the .bash_profile while others did not...
if [ -f ~/.git-prompt.sh ]; then
source ~/.git-prompt.sh
export PS1='Geoff[\W]$(__git_ps1 "(%s)"): '
fi
You've installed the version of git-completion.bash from master - in git's development history this is after a commit that split out the __git_ps1 function from the completion functionality into a new file (git-prompt.sh). The commit that introduced this change, which explains the rationale, is af31a456.
I would still suggest that you just source the version of git-completion.bash (or git-prompt.sh) that is bundled with your installation of git.
However, if for some reason you still want to use this functionality by using scripts separately downloaded from master, you should download git-prompt.sh similarly:
curl -o ~/.git-prompt.sh \
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
... and add the following line to your ~/.bash_profile:
source ~/.git-prompt.sh
Then your PS1 variable that includes __git_ps1 '%s' should work fine.
After upgrading to OSX 10.9 Mavericks I had to reference the following files to get git shell command completion and git prompt to work again.
From my .bash_profile or similar:
if [ -f /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash ]; then
. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
fi
source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
#shell prompt example
PS1='\u $(__git_ps1 "(%s)")\$ '
You should
$ brew install bash bash-completion git
Then source "$(brew --prefix)/etc/bash_completion" in your .bashrc.
Following worked for me like a charm:
Run following in your Terminal:
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
Open/Create bash_profile:
$ vi ~/.bash_profile
Add following to the file:
source ~/.bash_git
export PS1='\[\033[01;32m\]os \[\033[01;34m\]\w $(__git_ps1 "[%s]")\$\[\033[00m\] '
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUPSTREAM="auto"
Finally, source it using:
$ source ~/.bash_profile
This will solve the problem of bash: __git_ps1: command not found.
Also your prompt will change to "os ". To change "os" to something else, modify "os" string in export PS1 line.
Solution for MacOS Sierra and git version 2.10.1 <2017-2-06>
Step 1: Install the Git
You can skip this step if you already installed the latest git.
Download git package from browser https://git-scm.com/download/
Note: if you install with curl [option] https://... option to download, you would have to make sure your system support SSL. So for new comer, to download from browser and install directly from git installer is much easier.
Installation Verification:
Show where is your git directory at: which git
Show which version your git currently is: git --version current version should be 2.10.1.
Step 2: Add your git profile to your shell
Open your shell profile:
nano ~/.bash_profile or nano ~/.bashrc Depends on where your modification is.
Add the following code to the file:
source /usr/local/git/contrib/completion/git-completion.bash
source /usr/local/git/contrib/completion/git-prompt.sh
Note: git installation location changed from opt/ directory to usr/local/ after OSX upgrade to El Capitain, and this is why some of the old answer above doesn't work anymore in MacOS Sierra.
Add the following code to your PS1 configuration:
Option 1: add directly to your PS1: export PS1='\w$(__git_ps1 "(%s)") > '
I prefer this simple approach since I already know the .git-completion.bash is there in my home directory, and I can add other prompt format in the front of it. here is my personal prompt for your reference: export PS1='\t H#\! \u:\w$(__git_ps1 "{%s}") -->> '
Option 2: Add a selection script
if [ -f ~/.git-completion.bash ]; then
export PS1='\w$(__git_ps1 "(%s)") > '
fi
Save and use the profile: source ~/.bash_profile or source ~/.bashrc
Now you should see the git prompt working properly and shows which branch you are in right now.
High Sierra clean solution with colors !
No downloads. No brew. No Xcode
Just add it to your ~/.bashrc or ~/.bash_profile
export CLICOLOR=1
[ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh ] && . /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
export GIT_PS1_SHOWCOLORHINTS=1
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUPSTREAM="auto"
PROMPT_COMMAND='__git_ps1 "\h:\W \u" "\\\$ "'
I had same problem when upgrading to Yosemite.
I just had to modify ~/.bashrc to source /usr/local/etc/bash_completion.d/git-prompt.sh instead of the old path.
then re-source your . ~/.bashrc to get the effect.
__git_ps1 for bash is now found in git-prompt.sh in /usr/local/etc/bash_completion.d on my brew installed git version 1.8.1.5
this works in OS 10.8 in the .bash_profile
if [ -f ~/.git-prompt.sh ]; then
source ~/.git-prompt.sh
export PS1='YOURNAME[\W]$(__git_ps1 "(%s)"): '
fi
For macports I had to add: source /opt/local/share/git-core/git-prompt.sh to my ./profile
If you're hoping to use Homebrew to upgrade Git and you've let your system become out-of-date in general (as I did), you may need to bring Homebrew itself up-to-date first (as per brew update: The following untracked working tree files would be overwritten by merge: thanks #chris-frisina)
First bring Homebrew into line with the current version
cd /usr/local
git fetch origin
git reset --hard origin/master
Then update Git:
brew upgrade git
Problem Solved! ;-)
At least with Xcode 6, you already have git-completion.bash. It's inside the Xcode app bundle.
Just add this to your .bashrc:
source `xcode-select -p`/usr/share/git-core/git-completion.bash
Download the files git-prompt.sh and git-completion.bash from this Git completion
Rename the files.
Move those files to your home directory.
Add the source file in to the .bash_profile
source ~/git-completion0.bash
source ~/git-prompt0.sh
and four to trigger the code block.
I know it's not a real answer...
I had some strange issues with sourcing git-prompt.sh in my .bashrc so I started to look for other solution. This one: http://www.jqno.nl/post/2012/04/02/howto-display-the-current-git-branch-in-your-prompt/ doesn't use __git_ps1 and author claims it works also on Mac (for now it works perfectly on my Ubuntu and it's easy to tweak).
I hope it helps!
I was doing the course on Udacity for git hub and was having this same issue. Here is my final code that make is work correctly.
# Change command prompt
alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \ . (.*\)/(\1)/'"
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
export PS1='[\W]$(__git_ps1 "(%s)"): '
fi
source ~/.git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff
# '\W' adds the name of the current directory
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"
It works!
https://i.stack.imgur.com/d0lvb.jpg
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh -o ~/.git-prompt.bash
[[ -f ~/.git-prompt.bash ]] && . ~/.git-prompt.bash
# Available GIT_PS1 options/env vars
cat ~/.git-prompt.bash | grep GIT_PS1_ | sed -r 's,^\s*#.*,,' | grep -v -E '^$' | sed -r 's,^.*(GIT_PS1_[A-Z_]+).*,\1,' | sort | uniq | sed -r 's,^(.*)$,export \1=,'
export GIT_PS1_COMPRESSSPARSESTATE=
export GIT_PS1_DESCRIBE_STYLE=
export GIT_PS1_HIDE_IF_PWD_IGNORED=
export GIT_PS1_OMITSPARSESTATE=
export GIT_PS1_SHOWCOLORHINTS=
export GIT_PS1_SHOWDIRTYSTATE=
export GIT_PS1_SHOWSTASHSTATE=
export GIT_PS1_SHOWUNTRACKEDFILES=
export GIT_PS1_SHOWUPSTREAM=
export GIT_PS1_STATESEPARATOR=
for i in $(cat ~/.git-prompt.bash | grep GIT_PS1_ | sed -r 's,^\s*#.*,,' | grep -v -E '^$' | sed -r 's,^.*(GIT_PS1_[A-Z_]+).*,\1,' | sort | uniq); do varname=$i; declare -g ${i}=1; done
# P.S Above is just illustration not all config vars are [0/1].
# For more info:
cat ~/.git-prompt.bash | sed -r -n -e '1,/^[^\s*#]/p' | head -n -2
This one worked for me, and it has colored git output and an indicator in the prompt whether files have changed / been added, right baked into it:
GIT_PS1_SHOWDIRTYSTATE=true
. /usr/local/Cellar/git/1.8.5.2/etc/bash_completion.d/git-completion.bash
. /usr/local/Cellar/git/1.8.5.2/etc/bash_completion.d/git-prompt.sh
PS1='\[\033[32m\]\u#\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '
Be sure to use the correct path! I used homebrew to install git, use brew list git to get the path to your current installation.
Would be nice not to use a hard coded path, but don't know how to get the path to the current installation.
More infos here: http://en.newinstance.it/2010/05/23/git-autocompletion-and-enhanced-bash-prompt/
For git, there are /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh. And please look /etc/bashrc_Apple_Terminal too.
So, I put these in ~/.bash_profile:
if [ -f /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh ]; then
. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
export GIT_PS1_SHOWCOLORHINTS=1
export GIT_PS1_SHOWDIRTYSTATE=1
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }__git_ps1 '\u:\w' '\\\$ '"
fi
Yet another option I just installed on Mojave: magicmonty/bash-git-prompt
Run (brew update) and brew install bash-git-prompt or brew install --HEAD bash-git-prompt
Then to your ~/.bash_profile or ~/.bashrc:
if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
__GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
GIT_PROMPT_ONLY_IN_REPO=1
source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi
I'm happy.
Please not that, if you haven't installed git through Xcode or home-brew, you'll likely find the bash scripts haysclarks refers to in /Library/Developer/CommandLineTools/, and not in /Applications/Xcode.app/Contents/Developer/, thus making the lines to include within .bashrc the following:
if [ -f /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash ]; then
. /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
fi
source /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
You'll need those lines if you wish to use git-prompt as well.
[1]: https://stackoverflow.com/a/20211241/4795986
Copy/Download the following files and copy them to home directory: ~/
git-completion.bash
git-prompt.sh
For the bash_profile, add this at the beginning:
source ~/git-completion.bash
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
For more and easy downloads you could check this.

Resources