upgraded local git on Lion, now what?

2

1

I reformatted my machine with Lion which had this default version of git:

$ git --version
git version 1.7.7.5 (Apple Git-26)

I decided to upgrade git so I did:

$ brew install git
==> Downloading http://git-core.googlecode.com/files/git-1.7.10.4.tar.gz
######################################################################## 100.0%
==> make prefix=/usr/local/Cellar/git/1.7.10.4 CC=/usr/bin/clang CFLAGS=-Os -w -pipe -    march=native -Qunused-arguments LDFLAGS= install
==> make CC=/usr/bin/clang CFLAGS=-Os -w -pipe -march=native -Qunused-arguments LDFLAGS=
==> make clean 
==> Downloading http://git-core.googlecode.com/files/git-manpages-1.7.10.4.tar.gz
######################################################################## 100.0%
==> Downloading http://git-core.googlecode.com/files/git-htmldocs-1.7.10.4.tar.gz
######################################################################## 100.0%
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

The OS X keychain credential helper has been installed to:
  /usr/local/bin/git-credential-osxkeychain

The 'contrib' directory has been installed to:
  /usr/local/share/git-core/contrib
==> Summary
/usr/local/Cellar/git/1.7.10.4: 1173 files, 23M, built in 39 seconds

Unfortunately, git wasn't upgraded:

$ git --version
git version 1.7.7.5 (Apple Git-26)

I'm new to Terminal/Command line and all things Unix so I'm not sure of what my next move is. I'm guessing "Caveats" is telling me that I should do something cause this upgraded git is not where the default git is. So, do I need to add these paths to my PATH? If so, how do I do this? If not, what do I do now?

Thanks

AdamT

Posted 2012-06-09T05:05:45.413

Reputation: 893

Answers

5

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:

  1. create a new directory, e.g. ~/bin
  2. put this directory at the front of PATH instead of /usr/local/bin
  3. 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.)
  4. 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!

Owen S.

Posted 2012-06-09T05:05:45.413

Reputation: 288

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.193

Yes, 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.747

Related 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.270

Keys 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.897

Btw, 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

3

I was having the exact same problem. Owen had the most complete overall answer.

That said I did some more digging and found what I believe to be a much easier way.

environment.plist builds the path it uses from /private/etc/paths

So, in terminal just type sudo nano /private/etc/paths and enter your root password.

Once you're in nano, you'll probably see /usr/bin first, and /usr/local/bin last. (Note: Everything needs to be on a separate line!) All I did was type in /usr/local/bin first and then remove it from the last line.

Exit terminal, then logout and log back in.

Go back into terminal and type git --version and you should be golden.

mike

Posted 2012-06-09T05:05:45.413

Reputation: 31

accepted answer didn't work for me, however this did. – dangerousdave – 2012-10-01T19:51:50.277