Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Replace a Git installation in /usr/bin/ with symlinks to a preferred git installation.
#!/usr/bin/env python -tt
"""Xcode4 installs and expects to find a git installation at /usr/bin.
This is a pain if you want to control your own installation of git that
might be installed elsewhere. This script replaces the git files in
/usr/bin with symlinks to the preferred installation.
Update the 'src_directory' to the location of your preferred git installation.
import sys
import os
#- Configuration -----------------------------------------------------------------
src_directory = '/usr/local/git/bin/' # preferred installation
dest_directory = '/usr/bin/'
files = ('git','git-cvsserver','git-receive-pack','git-shell','git-upload-archive','git-upload-pack','gitk')
def main():
if os.getuid():
print "This script needs to be run as 'sudo python'"
for a_file in files:
src_file = os.path.join(src_directory, a_file)
dest_file = os.path.join(dest_directory, a_file)
if os.path.exists(dest_file):
os.symlink(src_file, dest_file)
if __name__ == '__main__':

Abizern commented Jan 11, 2011

Notice the 'sudo' at line 18? That means you'll have to enter your password. No need to panic, I'm not trying to spoof you but need this to modify files in '/usr/bin'.

caius commented Jan 11, 2011

Why not just have the script do an ln -nfs and make the user invoke it with sudo ./ Means they know it's sudo asking for the password, not your script (if they can't read .py)

caius commented Jan 11, 2011

Also, this should do the same job:

#!/usr/bin/env zsh
for i in $FROM_DIR/git*; ln -nfs $i $(echo -n $i | sed -Ee "s#$FROM_DIR#$TO_DIR#")

Wondered why my git is coming from /usr/local/bin as per installed by homebrew, turns out I put /usr/local/bin before /usr/bin in my PATH, so no need to fiddle the symlinks, the shell picks homebrew's git to use already. And the git commands in /usr/bin are what Xcode expects them to be (shouldn't matter that much, but leaving them unchanged is always nice.)


Abizern commented Jan 11, 2011

Thanks for the Shell script. I wanted an excuse to play about with Python.

iamleeg commented Jan 11, 2011

What caius said: don't do sudo inside the script, require that it gets called with sudo. Also, consider a "and your original files have been moved to ${SAFEPLACE}" option :-D


Abizern commented Jan 11, 2011

Applied some suggestions.

Script requires sudo to run and uses the OS module to create the symlinks.

theogfx commented Sep 6, 2011

Just a small note : caius' script does the contrary of the python script.
If you want to update git and use it as default, FROM_DIR and TO_DIR should be exchanged in the zsh script.

Thanks, maybe one day xcode will update their git..


Abizern commented Oct 24, 2011

The version of Git that comes with Xcode is fairly recent. This script is just for those who want to use the development builds of Git.

Can any one help me. I am setting up my local development environment. I have a server (MAC OS X Lion Server) and want that to house git and use the repository over http. I followed all the steps from but now I get fatal: http:/myname@server/repo/test-project.git/info/refs not found did you run git update-server-info on the server? Please help i am running out on time... I want to use git and start this project...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment