43

I've installed XCode and therefore git is there as well. Since i want to have a newer version of git I installed using homebrew.

But the homebrew version of git is never called since my PATH looks like this

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

which means the /usr/bin/git is allways called before /usr/local/bin/git.

Is there a way to change that without changing the PATH?

Michael Gogel
  • 303
  • 3
  • 7
BetaRide
  • 16,207
  • 29
  • 99
  • 177
  • I created this script to have Xcode use your local installation of Git in /usr/local/bin. Check it out. https://gist.github.com/4659915 – Baub Jan 28 '13 at 22:45

3 Answers3

58

Xcode is actually using the GIT that is stored in /Applications/Xcode.app/Contents/Developer/usr/bin. The same version of GIT gets installed in /usr/bin when you installed the command line tools as part of Xcode installation. So, you won't be able to change what Xcode is using (unless you are willing to muck with the contents of the Xcode package). If, from a terminal application, you want to use the homebrew-installed GIT then you have two options:

  1. Reference GIT with the full path as /usr/local/bin/git ... For this case you can create an alias like alias mgit=/usr/local/bin/git and then use mgit ... from the terminal
  2. Change the path as PATH=/usr/local/bin:$PATH either in your .bashrc or .zshrc if you use zsh file (or each time you start a terminal at the command line).
GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • 1
    Thanks for the answer. After all I changed the `PATH` in `/etc/paths` . – BetaRide May 05 '12 at 15:37
  • Related: http://stackoverflow.com/questions/5364614/is-there-a-problem-with-having-usr-local-bin-before-usr-bin-on-the-path-in-os – Matt Montag Oct 31 '12 at 18:11
  • 4
    On OS X Mountain Lion, I use ".bash_profile" instead of ".bashrc" - I've been doing this for years, though I don't know where I learned it, or why it works (and .bashrc doesn't.) – Andrew Theken Nov 10 '12 at 23:36
  • 1
    The name of the alias can still be just `git`, hasn't caused any problems for me and you get to keep the original name. – Jeffrey Martinez Feb 19 '15 at 06:46
14

Since Xcode hard coded its own version of git which is installed on /Applications/Xcode.app/Contents/Developer/usr/bin/git, I managed to use this work around trick:

  1. change into the Xcode directory:

    cd /Applications/Xcode.app/Contents/Developer/usr/bin

  2. rename the Xcode's git like this:

    sudo mv ./git ./git-xcode-usr-bin

  3. link my own git which is installed through homebrew:

    sudo ln -s /usr/local/bin/git ./git

And I did the same thing with /usr/bin/git

This will acctually link /usr/local/Cellar/git/1.8.0/bin/git (because I'm use git 1.8.0 at the present)

Certainly this may cause some other problems, such as when I upgrade the homebrew's verion git in the future, it would not work for me :( and I have to make a new link then.

I do it like this because I want to solve my own problem here 13177203. And after search StackOverFlow for a long time, I finally got this solution.

Community
  • 1
  • 1
oppih
  • 860
  • 8
  • 14
  • 2
    While in general I avoid messing with packages like this, I did use this answer. Because of the change in `push.default` options in git 1.8, Xcode was giving me the following error and not allowing me to add assets to my project: `error: Malformed value for push.default: simple. error: Must be one of nothing, matching, tracking or current. fatal: bad config file line 7 of .gitconfig`. The other option would have been to change my gitconfig back to the 1.7 settings. – towynlin Nov 30 '12 at 16:49
8

If you are using fish shell instead of bash, you can point to your preferred git binary by adding the following to ~/.config/fish/config.fish.

function git
  /usr/local/bin/git $argv; 
end
Paul Wenzel
  • 1,886
  • 2
  • 15
  • 15
  • Worked like a charm! Thanks for this tip. I didn't know you could do this in fish shell. It's very useful. – leifericf Aug 21 '13 at 10:37