That's correct. /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:
which git
and see which directory Git is currently being found in. That's the directory you have to beat. Also type this:
echo $SHELL
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 ~/.bash_profile
(where ~
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:
export PATH=/usr/local/bin:$PATH
(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:
echo $PATH
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.
~/bin
- put this directory at the front of PATH instead of
/usr/local/bin
- 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 git
that 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
git
.
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:
echo $PATH
After that, run this:
defaults write ~/.MacOSX/environment PATH ...
where ...
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!
All very true, but if the OP has a working installation of Homebrew,
/usr/local/bin
should be in their path already. – slhck – 2012-06-09T09:07:33.193Yes, but in my default PATH it's near the end. If there's a Git in
/usr/bin
it will win. – Owen S. – 2012-06-09T17:45:00.747Related question - I upgraded to Lion after installing Homebrew / Git previously on Snow Leopard. Trying to keep it concise, I need to nuke everything and start over. If I delete
/usr/bin/.git/
Will I lose my RSA keys / personal config Git settings? – Brian – 2012-06-09T21:03:47.270Keys are usually kept in
~/.ssh
. Your global Git config is usually in~/.gitconfig
. I've never heard of/usr/bin/.git
, and/usr/bin
isn't where I understood Homebrew installed things, so I'm not sure what's up there. Sorry! – Owen S. – 2012-06-10T00:26:43.897Btw, cleaning of Homebrew is covered here: http://superuser.com/questions/203707/how-to-uninstall-homebrew-osx-package-manager
– Owen S. – 2012-06-10T00:38:08.450