How to properly install Git on OS X

There is a problem with installing Git on OS X: it is already installed on the system and you just can't update it. Each time the OS updates, it installs it again.

For example, at the time of writing this question, the current version of Git is 2.5.1. And OS X Yosemite is 2.3.2.

When installing a new version via Homebrew, it still remains unavailable.

 6
Author: Nick Volynkin, 2015-09-04

1 answers

Installation "from scratch"

  1. Installing Git, for example via Homebrew

    $ brew install git
    
  2. Checking the current version of Git

    $ git --version
    git version 2.4.9 (Apple Git-60)
    

    If you see "Apple Git", it means that the old version is used. Let's check where the used version of Git is installed.

    $ which git
    /usr/bin/git
    

    Exactly, this version is put together with the system. And the one that we need, and that is installed by homebrew, is in /usr/local/bin/git. Why isn't she chosen? Most likely because the path to it is located further away in the environment variable $PATH.

    echo $PATH
    
  3. Changing $PATH

    The $PATH variable stores folder paths separated by colons. When you want to run a program by name (rather than by full path), the search takes place in all these folders in turn, from left to right. The variable is set in the file ~/.bashrc (or ~/.zshrc, etc., depending on the shell used). Open it and find something like this line:

    export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
    

    Put /usr/local/bin earlier than /usr/bin:

    export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    

    Restart the shell:

    bash
    $ git --version
    git version 2.10.1
    
    $ which git
    /usr/local/bin/git
    

    Hooray, it works!

 7
Author: Nick Volynkin, 2016-10-12 16:14:00