How to upgrade Git on Mac (OSX)

Recently a vulnerability in Git was discovered that could lead to arbitrary code execution when a developer utilizes a malicious repository. The good news is that this issue has been patched and the fix is available in version 2.17.1.

Now let’s upgrade!

Overview

In this guide we are going to walk through how to upgrade Git via Homebrew. First we will install Homebrew and then install Git. Given that you may have Git already installed (e.g. via XCode), this guide will also walk you through how to change your path to use the official (non-Apple) distribution.

A few details before we get started:

If you are unfamiliar with Homebrew, it is a package manager for Mac that allows you to easily install and utilize a myriad a programs (i.e. formulae).

  • For this guide, I am assuming you are running MacOS 10.11 or higher
  • Additionally, check out the full Homebrew system requirements here before diving in

Step by Step Guide

Install Homebrew

if Homebrew is already installed, you can skip to the Git section

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

For installation details, including system requirements check out the documentation here

Update Homebrew to ensure you are on the latest version

brew update

Run brew doctor to ensure everything is good to go

brew doctor

If all is well, the output should say Your system is ready to brew.

Now let’s get to Git.

Check your current version of git

git --version

If the version of git says something like git version 2.15.1 (Apple Git-101) then you are running the Apple version of Git, not the official distribution.

No worries, Homebrew has got us covered.

Install git via Homebrew

brew install git

Change your local path to the Homebrew version

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

Check the git version

git --version

You should now see the current version of git, such as git version 2.17.1.

To upgrade in the future, simply run

brew upgrade git

Conclusion

We are all set! Homebrew is installed, Git is installed, and we have changed our path to point to the current version. For further details on Homebrew and some additional resources, check out the links below.

Additional Resources

Updated: