Skip to content

Instantly share code, notes, and snippets.

@brandonsimpson
Last active November 5, 2024 17:53
Show Gist options
  • Save brandonsimpson/54d9e085c9fde5e6ad3a to your computer and use it in GitHub Desktop.
Save brandonsimpson/54d9e085c9fde5e6ad3a to your computer and use it in GitHub Desktop.
Re-installing Git on Mac OSX with Brew

Re-installing Git on Mac OSX with Brew

This is helpful if you've previously installed git from source on OSX, and other compilers can't find the correct path. You need to remove the current version of git, then re-install with brew.

Uninstall git if installed manually

  1. Check which git you're running:

    which git
    

    output should be similar to this: /usr/bin/git

  2. Remove that git install

    sudo rm -rf /usr/bin/git/
    sudo rm /etc/paths.d/git
    sudo rm /etc/manpaths.d/git
    sudo pkgutil --forget --pkgs=GitOSX\.Installer\.git[A-Za-z0-9]*\.[a-z]*.pkg
    
    
    

Re-install git with brew

brew uninstall git
brew update
brew install git

Check which git you're now running:

which git

Should now say: /usr/local/bin/git

@tony-sol
Copy link

tony-sol commented May 6, 2022

Afterall, what's point of using brew-ish git instead of provided by default, except version?

@interglobalmedia
Copy link

interglobalmedia commented Jul 14, 2022

sidonaldson I went with your suggestion to avoid any potential issues and to save time, and the alias approach works beautifully. SIP is still going strong with Monterey. I researched M1 devices, and apparently it is even worse. I have that to look forward to in my near future!

@danicodeve
Copy link

On Apple Silicon the correct alias is

alias git='/opt/homebrew/bin/git'

@PetterAxcell
Copy link

I have gone through the steps you listed in this instruction set, however once I run the "sudo rm -rf /usr/bin/git/" I get the error

rm: /usr/bin/git: Operation not permitted

I am currently running MacOS Mojave v.10.14.6.

and I have Xcode installed (if that means anything).

Please advise. -Pete8451

I uninstall xcode and git works better

@pakshalghiya
Copy link

@akshajmody

Hi guys, I am having a similar issue. I checked my git version and it was 2.23.0, I did brew install git which should have updated to 2.28.0 I believe however when I restarted my mac and checked git version I still see 2.23.0. I checked which git and it shows "/usr/local/bin/git". How do I know if I am using the right git on my system?

IMPORTANT EDIT - Any solution I wrote in this comment should be ignored unless you've already done the following: restarted your terminal after running brew install git (important!), ensured which git is returning /usr/local/bin/git and that this is what brew installed, confirmed /usr/local/bin/git --version is displaying the new version installed via brew, and if git --version is still returning the old Xcode version even after restarting your terminal then you can either use simple workarounds to add the path to the proper git install as others and myself mentioned (shown below this edit) or you can use people's solutions above to remove Xcode' git . However it's unlikely any of this is necessary with newer versions of macOS if you've restarted the terminal after installing git with homebrew. For details see @martbe's comment and my comment below it that explains where confusion can happen after installing git with homebrew alongside Xcode's git. For newer versions of macOS, the problem that myself and possibly others experienced is due to simply not restarting the terminal after running brew install git.

See above edit before reading further.

If you do which -a git you'll notice there are multiple versions of git installed assuming you have one installed from XCode (or straight from git) and another that you've just installed from homebrew.

e.g.,

$ which -a git
/usr/local/bin/git
/usr/bin/git

The one in /usr/local/bin/git is the one from homebrew which you can confirm with ls -al /usr/local/bin/git and you'll see it's symlinked to the git binary installed in homebrew's Cellar directory. You can alternatively confirm by passing --version to each absolute command path to see which is which.

When you run a non-absolute command like git it will search directories in the $PATHenvironment variable and use the first command it finds so you should run echo $PATH and ensure that /usr/local/bin comes before /usr/bin, and if it does then running git should run the correct command for you. Btw, macOS sets up the $PATH variable this way by default but if you've changed your $PATH just ensure that /usr/local/bin comes first.

If the above is true and $PATH is correct but git --version is still running the /usr/bin/git command then the quickest and simplest fixes are to either:

  • Set an alias like somebody else said by adding alias git="/usr/local/bin/git" to ~/.bash_aliases.
  • Add the correct git command to $PATH by adding export PATH=$PATH:/usr/local/bin/git

If that case is happening where $PATH is correctly loading from /usr/local/bin first but git is still running the /usr/bin/git command, I think this may have to do with Xcode overriding things but somebody else who's more knowledgeable of Xcode can confirm this since I'm not sure. I've just seen odd things like this before, and it would explain some of these weird cases (and I know Xcode has things like xcrun to find tools). I'm just not sure. Edit - This could also be related to @martbe's point about reloading terminal, even for older versions, not sure. I'm leaving this be now lol. I've done enough damage!

But I am getting this Path: /opt/homebrew/bin/git

@dfyz011
Copy link

dfyz011 commented Sep 22, 2023

Don't forget to reload your terminal after install git with brew. (For me helped to restart vscode)

@nnat157
Copy link

nnat157 commented Nov 21, 2023

Hey people! I was able to get this issue resolved. However i had to install manually (not using brew initially).

I followed the steps from the Git website to reinstall Git. https://git-scm.com/book/en/v2/Getting-Started-Installing-Git It automatically updated the git version i had on my Mac. Having XCode on my device did not effect the install. I know this may seem broad, but it worked for me!! hope this helps!

Thank you so much!! This absolutely worked for me!

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