DEV Community

Cover image for How to upgrade Git to latest version on macOS

How to upgrade Git to latest version on macOS

πŸ‘€ If you have homebrew installed, follow there steps, if not, skip to the second part

1. Confirm existing git

Out of curiosity, if you want to check where your Git is installed, you can type the following in your terminal:

which git
Enter fullscreen mode Exit fullscreen mode

It should return something like: /usr/bin/git

2. Check git version

To check the version of Git you are using, type:

git --version
Enter fullscreen mode Exit fullscreen mode

3. Install Git Using Homebrew

If you want to update your Git version, it's simple if you already have Homebrew installed. Assuming Homebrew is already set up on your system, type the following:

brew install git
Enter fullscreen mode Exit fullscreen mode

This will install the latest version of Git and automatically set it in your PATH, replacing the Apple-provided version.

Now quit and restart your terminal.

4. Confirm Installation Location

To verify where the new version of Git is installed, run:

brew info git
Enter fullscreen mode Exit fullscreen mode

The output should look something like this: Installed
/opt/homebrew/Cellar/git/2.47.1 (1,685 files, 54.4MB) *

You can also check the installed git version and path:

git --version
which git
Enter fullscreen mode Exit fullscreen mode

The output should point to the new version, located at /usr/local/bin/git or /opt/homebrew/bin/git.

5. Update PATH to Prioritize the New Git Version

If the output of the above command shows the latest version then you are all set.

⚠️However, it's possible that it is still using the previous version.

If macOS continues to use the system-provided /usr/bin/git instead of the new version, you need to prioritize the Homebrew version by updating your PATH.

For Intel Macs:

Add this line to your shell configuration file (e.g., ~/.zshrc, ~/.bashrc):

export PATH="/usr/local/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

For Apple Silicon Macs:

Add this line instead:

export PATH="/opt/homebrew/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

Then reload your shell configuration:

source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Finally, verify the installed version again:

git --version
Enter fullscreen mode Exit fullscreen mode

If the output shows the latest version, you're all set!


πŸ‘‰ If you don't have homebrew installed, follow these steps to install homebrew

If Homebrew is not installed, run the following command to set it up:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

After installation, confirm Homebrew is recognized by running:

brew --version
Enter fullscreen mode Exit fullscreen mode

Set Homebrew Path Variable

If your terminal does not recognize brew, add Homebrew's bin directory to your PATH:

echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

This command appends the line export PATH="/opt/homebrew/bin:$PATH" to your .zshrc file, ensuring that the /opt/homebrew/bin directory is included in your PATH variable.

After running this command, you need to reload your .zshrc file to apply the changes:

source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

This ensures that the changes take effect immediately without needing to restart your terminal session.

Now you can install Git using Homebrew (see Step 3 above).

Top comments (0)