Skip to content

Instantly share code, notes, and snippets.

@brandonsimpson
Last active November 21, 2023 09:45
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • 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

@martbe
Copy link

martbe commented Jan 2, 2021

Screenshot 2020-12-31 at 8 55 45 PM

this is what I getting after doing those steps please help.

See my contribution above.

@codeinaire
Copy link

I've got a minor addition for convenience sake.

Instead of shutting down your terminal after running brew install git simply run source <your shell config file>.

This will refresh your CLI. Then you can run which git and you'll get the path to the git installed by brew.

Example: source .zshrc if you are using Z shell.

@Ciara2-0
Copy link

sudo rm -rf /usr/bin/git wont work for El Capitan due to SIP restriction
simple solution is create alias
alias git='/usr/local/bin/git'

brew does this anyways :)

Thanks! It worked!

@PoloskounTheGracious
Copy link

PoloskounTheGracious commented Sep 7, 2021

Doing the following worked for me in the following situation:

  1. I had Apple's stock git [git version 2.21.1 (Apple Git-122.3) - (on MacBook Pro Big Sur v11.5.2 on 9/7/2021)
  2. I ran brew install git on my user's base directory (the directory you get to if you run cd by itself) - Not sure if brew installs it to current directory or somewhere specifically, I only know that Git's official website suggests cloning, which is directory sensitive so I did this because I didn't have much luck doing it right via Git's github clone).
  3. I then ran "git --version" and realized that my computer was still in bed with Apple Git-122.3 which is not what I wanted.
  4. I then ran: alias git='/usr/local/bin/git'
  5. Finally, I ran git --version again to check and the intended effect was achieved, I am now on git version 2.33.0 as intended.
  6. I also ran which git to check that as well and all is good, now I just have Apple's older git somewhere on my SSD but I don't really give a crap, the new one works as intended so I'll sacrifice that tiny bit of uselessly spent memory lol.

@MosheAvdiel
Copy link

updating brew and upgrading fixed the push in my case. (didn't uninstall or remove anything).

@maestroh1git
Copy link

I really want to remove apple Git
I am trying to install homebrew to my mac with
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

but, I keep getting this

amaku@Davids-MacBook-Pro ~ % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> Checking for `sudo` access (which may request your password)...
Password:
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew

Press RETURN to continue or any other key to abort:
==> /usr/bin/sudo /usr/sbin/chown -R amaku:admin /opt/homebrew
==> Downloading and installing Homebrew...
fatal: remote error: 
  The unauthenticated git protocol on port 9418 is no longer supported.
Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ for more information.
Failed during: git fetch --force origin

I've tried changing the git url replace insteadOf git:// for https://

And now I just did this, trying to uninstall the git, maybe homebrew's installation will force a git install or something

amaku@Davids-MacBook-Pro ~ % sudo rm -rf /usr/bin/git/
Password:
rm: /usr/bin/git/: Not a directory
amaku@Davids-MacBook-Pro ~ % sudo rm /etc/paths.d/git 
rm: /etc/paths.d/git: No such file or directory
amaku@Davids-MacBook-Pro ~ % 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

rm: /usr/bin/git/: Not a directory
rm: /etc/paths.d/git: No such file or directory
rm: /etc/manpaths.d/git: No such file or directory
zsh: no matches found: --pkgs=GitOSX.Installer.git[A-Za-z0-9]*.[a-z]*.pkg
amaku@Davids-MacBook-Pro ~ % which git
/usr/bin/git

homebrew still not installing

@maestroh1git
Copy link

I found the answer in Homebrew/discussions#2899

I ran git config --global --edit then I deleted everything (there were a lot of instead of declarations) and saved the file.

Then I did the homebrew installation and it has installed fine!

@MosheAvdiel
Copy link

MosheAvdiel commented Apr 28, 2022 via email

@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