brew doctor Warning: How to add git to PATH?

I found the solution to my problem. It was caused by a wrong fileformat of /usr/local/Library/ENV/scm/git:

$ /usr/local/Library/ENV/scm/git --version
-bash: /usr/local/Library/ENV/scm/git: /bin/sh^M: bad interpreter: No such file or directory

So I edited the fileformat:

$ vi /usr/local/Library/ENV/scm/git

Press 'ESC', write :set fileformat=unix, Press 'Enter', write :wq!.

That fixed it for me.


For me this was caused by missing xcode tools, fixed by xcode-select --install.

Several other suggested fixes including git fetch in the brew directory and/or changing git core.autocrlf did not fix this.

In more detail I uninstalled git from brew brew uninstall --force git, but Apple's own git then didn't work (/usr/bin/git --version reported missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun).

Running brew doctor suggested running xcode-select --install, I ran this and it fixed Apple's git, and also fixed brew. (Possible to then reinstall git via brew brew install git.)


Simply run:

export PATH=/usr/local/bin:$PATH

Your PATH variable should look something like

PATH="/usr/bin:/usr/local/bin"

First, change directory to /usr/local/Homebrew in the Terminal (for older versions of MacOS, use cd cd /usr/local/Library/Homebrew). Then run:

git pull origin master
brew update && brew upgrade