xcodegithomebrewversion

Git latest version installed using homebrew not getting used in mac. Instead the Xcode one gets used, despite adding to the path variables


I have to update git to the latest version. I updated the brew version of git to latest one and used the unlink command. I have the xcode version installed as well. I have tried setting up the path variables but am not able to update the order of path variables to pick up

I did that using the "brew upgrade git"

brew upgrade git
==> Downloading https://formulae.brew.sh/api/formula.jws.json
######################################################################### 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
######################################################################### 100.0%
Warning: git 2.47.0 already installed

I ended the terminal and opened a new one. Now when I check the version, I am getting

git --version
git version 2.39.5 (Apple Git-154)

I checked the locations of git using which, and got the below results

which -a git
/usr/bin/git
/opt/homebrew/bin/git
/usr/bin/git

When I check the PATH using echo, I am getting "/usr/bin/git" before "/opt/homebrew/bin/git"

/usr/bin:/usr/local/bin:/opt/homebrew/bin:

I want to use the git in homebrew, but am not able to do that. I tried updating the order of the locations taking reference from this article. https://codewithsusan.com/notes/path-variable-mac

I found the directories to be located in "sudo nano ~/.bashrc" But after updating, ending the terminal and opening again I still get the paths as

tr ':' '\n' <<< $PATH

/usr/bin
/usr/local/bin
/opt/homebrew/bin
/opt/homebrew/sbin
/usr/local/bin

I am not able to understand what I am missing, so that git command picks up the brew installed one.


Solution

  • OK, let's debug this. We first need to ask a set of questions.

    1. What shell are you running? Why? This defines how $PATH is set.
    2. As of MacOSX Catalina (2019), the default shell is zsh, not bash
    3. Where is the path being set...

    Determine Shell

    > echo $SHELL
    > /bin/zsh
    

    The above is what I would expect to see. Can you confirm this?

    Determine the git being used?

    On macOS, type is preferred over which - it provides more detailed information about how the shell interprets a command. type can identify whether a command is an alias, a shell function, a built-in, or an external executable. It shows how the shell will interpret the command. My system returns (I set it to do this):

    > type git                                                         
    git is an alias for noglob git
    git is /usr/bin/git
    git is /opt/homebrew/bin/git
    > git --version
    git version 2.39.5 (Apple Git-154)
    

    Check the path

    Let's modify the $PATH to see if we can fix this.

    > export $PATH=/opt/homebrew/bin/git:$PATH
    
    > type git                                                         
    git is an alias for noglob git
    git is /opt/homebrew/bin/git
    git is /usr/bin/git
    > git --version
    git version 2.47.0
    

    Resolution

    If your system is running zsh, i.e. $SHELL returns zsh then you need to look in:

    now if $SHELL returns bash, then you need to look in:

    Wrap-up

    My gut feeling is that once you determine your shell, you can debug this.

    Citations