MacOS – Broken Git installation on the Mac. How to fix it

gitinstallmacosterminalxcode

I've had git running on my MacBook Pro (macOS High Sierra 10.13.6) for ages. I recently uninstalled Xcode because I never use it, but that seems to have broken Git and I can't figure out how to fix it.

If I open a terminal window and type git --version, I get the following error:

xcrun: error: active developer path ("/Applications/Xcode.app/Contents/Developer") does not exist
Use `sudo xcode-select --switch path/to/Xcode.app` to specify the Xcode that you wish to use for command line developer tools, or use `xcode-select --install` to install the standalone command line developer tools.
See `man xcode-select` for more details.

If I then try Xcode-select –install, I get:

xcode-select: error: command line tools are already installed, use "Software Update" to install updates

Software Update shows no updates are available. I tried to reinstall Xcode from the App Store, but I'm getting:

Xcode can’t be installed on “Macintosh HD” because macOS version 10.14.3 or later is required.

I can't upgrade my macOS version because it says no updates are available!

I tried installing git from the downloadable package, but that made no difference (it seemed to install ok, but I don't know where it installed and it's still looking for the old version). Then tried Homebrew, which again seemed to work but made no difference, so I uninstalled it.

I don't know what to try next! I don't really know what I'm doing with macOS and I'm a bit concerned I'll break stuff if I fiddle too much. Can anyone help?

Best Answer

To debug this, start by executing:

type -a git

and

echo $PATH

In OP's case, running the above commands gave the following output respectively:

git is /usr/bin/git
git is /usr/local/bin/git

and

/usr/local/opt/php@7.0/sbin
/usr/local/opt/php@7.0/bin
/Applications/MAMP/bin/php/php7.1.8/bin
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
/usr/local/autoconf
/opt/X11/bin
/usr/local/share/dotnet
~/.dotnet/tools
/Library/Frameworks/Mono.framework/Versions/Current/Commands
/Applications/Xamarin Workbooks.app/Contents/SharedSupport/path-bin
/Applications/Android Studio.app/Contents/plugins/flutter/bin

(in the above output is formatted for readability by replacing : with newline)

Now, run the following commands (using absolute path) to check if git package is actually installed:

/usr/bin/git --version

and

/usr/local/bin/git --version

The above gave the following output, respectively:

active developer path does not exist

and

git version 2.21.0

This indicates that Homebrew installed copy is still present in the system. However, Xcode/system installed copy (which should take precedence due to $PATH variable configuration) wasn't accessible because developer path couldn't be found.

To resolve the issue and to use the Xcode/system installed copy of git, run:

xcode-select --reset

This will resolve the error.

To use the latest version of Git, install the same via Homebrew by running:

brew install git

and set the PATH variable so that /usr/local/bin takes precedence over /usr/bin.