/usr/local/bin or equivalent needs to be added to your
PATH, because that's where
brew installs software. Furthermore, it will need to be ahead of whatever directory the default Git is in. (Note: I'm going to go slow through this, just to ease you into the Unix world a bit.)
Before you make a move to fix PATH, do this in Terminal:
and see which directory Git is currently being found in. That's the directory you have to beat. Also type this:
Hopefully you see '/bin/bash', since that's what I'm going to assume you're using. If not, let us know.
The easiest thing to try is simply to put
/usr/local/bin at the front of the PATH. This is traditionally done in a config file for your shell that gets run when you fire up a Terminal. For the bash shell, it's
~ is the nickname for your home directory). Go ahead and create a text file there if you don't have it already, and add this line:
(Note: use a proper bare-bones text editor here, like TextWrangler or Xcode or your favorite programmer's editor, instead of TextEdit which likes to save RTF files.)
The ':' separates directories in the PATH variable, and
$PATH expands to the current (pre-assignment) definition of this environment variable. In sum, this command prepends
/usr/local/bin to your PATH, then exports the updated PATH to your environment, so that other programs run from the shell can see and use the updated PATH. (Note: It's OK if
/usr/local/bin comes later in the PATH as well. You can remove the second copy, and set PATH to the completely doctored-up value, if you decide you want to keep
/usr/local/bin up front.)
Save that file, quit Terminal, and restart it. Create a new Terminal window and type:
You should now see
/usr/local/bin on the front. Run
git --version and make sure you're seeing the version you expect.
Having done this, what could go wrong? Well, in some cases you might have installed something in
/usr/local/bin that's not quite as stable as your standard system applications, and so you may not want everything in that directory to take precedence over the standard PATH. If this happens to you, a common solution is to make a new directory that only has references to the specific programs you want to come up before system-standard programs in the search:
- create a new directory, e.g.
- put this directory at the front of PATH instead of
- in the Terminal, run
ln -s /usr/local/bin/git ~/bin/git to create a symbolic link called
git in the new directory, pointing at the
brew installed. (Symbolic links are akin to "shortcuts" in the Windows world, if you're familiar with those.)
- quit and restart Terminal, and test to make sure the correct program you want is being selected when you run
I should also note that there's a more advanced, Mac-specific way to set up PATH. It involves creating/altering your
environment.plist configuration. To do this, first figure out the full path (i.e. not involving previous definitions of
$PATH) that you need:
After that, run this:
defaults write ~/.MacOSX/environment PATH ...
... gets replaced with whatever path you want to be the system-wide default. Put your directory in front (
/usr/local/bin or whatever), don't forget the ':', and copy/paste the echo'd value of PATH on the end. Then, to make sure your Mac config and your shell config are on the same page, go to ~/.bash_profile, and where you previously had the
export PATH line, do this instead:
export PATH=`defaults read ~/.MacOSX/environment.plist PATH`
This is how I set up PATH on my Mac.
Why go through all this trouble? Because, unlike the PATH defined in
~/.bash_profile, this default can be seen by GUI applications that are launched by the Finder or Spotlight. However, for Git operations you'll be running from the command line, you won't need it. Also, many GUI applications that use Git can also be given the absolute path (
/usr/local/bin/git) to the Git program, should they need to run it. So you might be able to get away without having to jump through these extra couple of hoops. But now you know the trick, just in case it does become necessary later on.
Happy Git hacking!