MacOS – Updating and running Git through Homebrew instead of XCode

bashgithomebrewmacosterminal

On my new 2014 Mac (OS X 10.9 Mavericks), I have just installed Git using Homebrew. In order to install Homebrew, I had to download and install XCode 5. So now, when I enter the command which git, I can see that Git is running from a different file pathway than the rest of the Homebrew installation. Git's different pathway is /usr/bin/git. I believe it is supposed to be in /usr/local/bin/ instead, with the rest of Homebrew.

I found a solution to getting Git to run from the Homebrew installation (and consequently an updated version of Git). According to another Q&A on this site, the solution is to add export PATH="/usr/local/bin:$PATH" in ~/.bash_profile.

Unfortunately, I don't understand where to enter that information. When I'm in Terminal, I can't change directories to something called "~/.bash_profile." How do I get there to enter the command export PATH="/usr/local/bin:$PATH"? I tried entering the command /usr/bin/open ~/.bash_profile, but Terminal tells me: The file /Users/**MYNAME**/.bash_profile does not exist.

Best Answer

This: ~/.bash_profile is a file, not a directory. It's a file that gets executed when you start a bash login shell:

A login shell is a bash shell that is started with - or --login.

When BASH is invoked as a login shell, the following files are executed in the displayed order.

  1. /etc/profile
  2. ~/.bash_profile
  3. ~/.bash_login
  4. ~/.profile

To ensure your Homebrew-installed tools appear before any other tools you can copy and paste the following in to a terminal window to update your ~/.bash_profile file:

cat << 'EOF' >> ~/.bash_profile

export PATH="/usr/local/bin:$PATH"
EOF

This will append (>>) the necessary lines to ~/.bash_profile for you in a safe manner. The append will create the file if it does not exist. The heredoc syntax used above is explained in this nice StackOverflow answer.

After making changes to your .bash_profile file you need to reload it or start a new Terminal session to see the change. To reload it in your existing session type:

source ~/.bash_profile