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

@Georgian
Copy link

rm: /usr/bin/git: Operation not permitted is because Mac's "System Integrity Protection".

Relatively complicated solution here: https://stackoverflow.com/a/53034855/1774643

@sjpedro
Copy link

sjpedro commented Nov 4, 2020

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!

this worked for me - now running: git version 2.27.0 Catalina 10.15.7

@johnpancoast
Copy link

johnpancoast commented Nov 24, 2020

@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!

@taejunoh
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?

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 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. I'm just not sure.

export PATH=$PATH:/usr/local/bin/git
This solution works for me. Thanks.

@martbe
Copy link

martbe commented Nov 25, 2020

Update for 2020

Ever since macOS Sierra, Mojave & Catalina with the most recent Homebrew simply do the following:

brew install git

That's it!

To confirm, close your terminal, reopen and run:
git --version

@johnpancoast
Copy link

johnpancoast commented Nov 26, 2020

Update for 2020

Ever since macOS Sierra, Mojave & Catalina with the most recent Homebrew simply do the following:

brew install git

That's it!

To confirm, close your terminal, reopen and run:
git --version

@martbe, Thank you! You have clarified the problem and I can see now why this has caused so much confusion. I'm going to edit my answer above.

The real culprit here is that the terminal needs to be reloaded after you run brew install git just like you said.

The reason this causes confusion is because it's not always necessary to restart your terminal when installing things with brew but this is a special case due to Xcode and the terminal where, even if your $PATH seems correct, the incorrect path to git will be used unless you restart your terminal. Re-sourcing ~/.bash_profile or the like won't work either. The terminal must be restarted after you run brew install git.

The longer explanation of how this can cause confusion is below.

In my case, since I've already installed homebrew's git, I want to get my system back to the original state so I can show where confusion happens when you install with homebrew. You may not yet have installed with homebrew so skip to the "Install Git with Homebrew alongside Xcode's Git installation" step below.

Uninstall brew's git:

$ brew uninstall git

Confirm that terminal sees old Xcode version of git.

$ git --version
git version 2.11.0 (Apple Git-81)

Install Git with Homebrew alongside Xcode's Git installation

Here it is assumed that we have a version of git that has been installed by Xcode and we want to install a new version using homebrew.

Install git with brew.

$ brew install git
...

Confirm that brew installed a new version of git... and here is where the confusion begins...

$ git --version
git version 2.11.0 (Apple Git-81)

Wait, what? This is the same old Xcode version we saw before.

It seems that brew didn't install a new version of git. Do some digging.

  • Run which git to see if new version of git is properly found in our path.

    $ which git
    /usr/local/bin/git
    

    Okay, that's correct path and the which command returns the first one found in your path so why wouldn't git --version work?

  • Dig a little further just to confirm that brew worked properly. Did brew install a new version of git?

    $ ls -al /usr/local/bin/git
    lrwxr-xr-x  1 [user] [group]  28 Nov 25 14:51 /usr/local/bin/git -> ../Cellar/git/2.29.2/bin/git
    

    -or-

    $ /usr/local/bin/git --version
    git version 2.29.2
    

    So it seems brew did install a new version of git. What's happening?

  • Since which git showed the above brew path to git, it should be the one that's used when running git --version, i.e., it should be the new version/same as above but we're still seeing same problem after we've confirmed brew installed git properly.

    $ git --version
    git version 2.11.0 (Apple Git-81)
    
  • Go a little further and check for all git programs in our path this time and see their versions.

    $ for i in $(which -a git); do echo -n "${i}:  "; ${i} --version; done
    /usr/local/bin/git:  git version 2.29.2
    /usr/bin/git:  git version 2.11.0 (Apple Git-81)
    
  • Ensure that /usr/local/bin is ahead of /usr/bin in $PATH even though we already know this to be the case.

    $ echo ${PATH}
    /usr/local/bin:/usr/bin:other_paths
    

    So our paths are correct which we essentially already knew...

This is where some people, including me, might set and forget something likealias git=/usr/local/bin/git to ~/.bash_aliases or add /usr/local/bin/git to $PATH without realizing that simply opening a new terminal and then running git --version will show the newly installed git from homebrew.

# New terminal window
$ git --version
git version 2.29.2

So for me, that's all that was needed all along. I can see from the above how it would be easy to get confused and simply set and forget an alias or equivalent, and that's still rather harmless, but it's still good to understand the real fix.

@johnpancoast
Copy link

johnpancoast commented Nov 26, 2020

export PATH=$PATH:/usr/local/bin/git
This solution works for me. Thanks.

@taejunoh Glad I could help but see my last answer. You might have encountered the same case as I did where the current terminal window where you ran brew install git will not recognize the proper path to the new git regardless of what's in $PATH.

You can test if you experienced the same as me by commenting that line you added/saving the file, opening a new terminal, and running git --version. If your case was anything like mine, you'll notice that it now works as expected without requiring any aliases or changes to $PATH. If your new terminal still doesn't display the new version then you can uncomment that line.

@martbe
Copy link

martbe commented Nov 26, 2020

Update for 2020
Ever since macOS Sierra, Mojave & Catalina with the most recent Homebrew simply do the following:
brew install git
That's it!
To confirm, close your terminal, reopen and run:
git --version

@martbe, Thank you! You have clarified the problem and I can see now why this has caused so much confusion. I'm going to edit my answer above.

The real culprit here is that the terminal needs to be reloaded after you run brew install git just like you said.

The reason this causes confusion is because it's not always necessary to restart your terminal when installing things with brew but this is a special case due to Xcode and the terminal where, even if your $PATH seems correct, the incorrect path to git will be used unless you restart your terminal. Re-sourcing ~/.bash_profile or the like won't work either. The terminal must be restarted after you run brew install git.

The longer explanation of how this can cause confusion is below.

In my case, since I've already installed homebrew's git, I want to get my system back to the original state so I can show where confusion happens when you install with homebrew. You may not yet have installed with homebrew so skip to the "Install Git with Homebrew alongside Xcode's Git installation" step below.

Uninstall brew's git:

$ brew uninstall git

Confirm that terminal sees old Xcode version of git.

$ git --version
git version 2.11.0 (Apple Git-81)

Install Git with Homebrew alongside Xcode's Git installation

Here it is assumed that we have a version of git that has been installed by Xcode and we want to install a new version using homebrew.

Install git with brew.

$ brew install git
...

Confirm that brew installed a new version of git... and here is where the confusion begins...

$ git --version
git version 2.11.0 (Apple Git-81)

Wait, what? This is the same old Xcode version we saw before.

It seems that brew didn't install a new version of git. Do some digging.

  • Run which git to see if new version of git is properly found in our path.

    $ which git
    /usr/local/bin/git
    

    Okay, that's correct path and the which command returns the first one found in your path so why wouldn't git --version work?

  • Dig a little further just to confirm that brew worked properly. Did brew install a new version of git?

    $ ls -al /usr/local/bin/git
    lrwxr-xr-x  1 [user] [group]  28 Nov 25 14:51 /usr/local/bin/git -> ../Cellar/git/2.29.2/bin/git
    

    -or-

    $ /usr/local/bin/git --version
    git version 2.29.2
    

    So it seems brew did install a new version of git. What's happening?

  • Since which git showed the above brew path to git, it should be the one that's used when running git --version, i.e., it should be the new version/same as above but we're still seeing same problem after we've confirmed brew installed git properly.

    $ git --version
    git version 2.11.0 (Apple Git-81)
    
  • Go a little further and check for all git programs in our path this time and see their versions.

    $ for i in $(which -a git); do echo -n "${i}:  "; ${i} --version; done
    /usr/local/bin/git:  git version 2.29.2
    /usr/bin/git:  git version 2.11.0 (Apple Git-81)
    
  • Ensure that /usr/local/bin is ahead of /usr/bin in $PATH even though we already know this to be the case.

    $ echo ${PATH}
    /usr/local/bin:/usr/bin:other_paths
    

    So our paths are correct which we essentially already knew...

This is where some people, including me, might set and forget something likealias git=/usr/local/bin/git to ~/.bash_aliases or add /usr/local/bin/git to $PATH without realizing that simply opening a new terminal and then running git --version will show the newly installed git from homebrew.

# New terminal window
$ git --version
git version 2.29.2

So for me, that's all that was needed all along. I can see from the above how it would be easy to get confused and simply set and forget an alias or equivalent, and that's still rather harmless, but it's still good to understand the real fix.

@johnpancoast No problem, it's caught me out numerous times and will no doubt do so again.

@smeeklai
Copy link

confirmed. brew install git worked after reboot

@johnpancoast
Copy link

johnpancoast commented Dec 15, 2020

@smeeklai, shouldn't need to reboot, though it won't hurt. You just need to restart your terminal as @martbe first pointed out (assuming you previously had Xcode's git installed).

I showed above the problems that happen and why that's necessary. I'm not sure the technical reasoning but I believe it has to do with Xcode's utilities seeking commands that essentially bypass what you have in PATH. Restarting the terminal fixes that.

So a restart of machine will work but all that is (or should be) needed is a restart of your terminal.

@gundappabaragalle
Copy link

Screenshot 2020-12-31 at 8 55 45 PM

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

@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