369

I just bought a new Mac with OS X Lion and I checked in the Terminal what version of git is installed by default. I got the answer

git --version
> git version 1.7.5.4

I would like to upgrade git to the latest version 1.7.8.3, so I downloaded the dmg installer "git-1.7.8.3-intel-universal-snow-leopard.dmg" and I launched it.

After the install, the Terminal still says that the version is 1.7.5.4. What am I doing wrong?

Nicolas Renon
  • 3,881
  • 2
  • 15
  • 13
  • 5
    Type `which git` to see where the binary is. Is that the same place you installed the new one? – Greg Hewgill Jan 22 '12 at 00:06
  • 4
    It says `/usr/bin/git` for the 1.7.5.4, but how can I know where the DMG installed the new version? Why it doesn't upgrade/overwrite the previous version automatically? If I want to use the new one how can I link the `git` command to the new version? (thanks for your really quick answer by the way!) – Nicolas Renon Jan 22 '12 at 00:14
  • 1
    on my mac (os 10.6.8) the old git 1.7.5.4 lives in /usr/bin/git while the new git installer 1.7.11.3 puts git into /usr/local/ – miker Sep 14 '12 at 15:45
  • I wonder if http://superuser.com/a/580611/74576 is a good approach. I wish I could figure out the commands for it. – Ryan May 15 '14 at 13:51
  • I made entry in .bash_profile instead of .bashrc & it worked. I think if your terminal is running as "Default Login Shell" then it happens. – Swapnil Chincholkar Jan 30 '15 at 04:15
  • `set -x PATH (brew --prefix git)/bin $PATH` if youre using fish instead of bash. Thanks for your answer btw – mmln Jan 20 '17 at 16:09

18 Answers18

375

It's simple if you already have Homebrew: Assuming you have homebrew installed, type the following:

brew install git

This should automatically install git and set it in your path, replacing the Apple one.

Now quit and restart your terminal.

Then check git version to confirm.

git --version

If the output of the above command shows the latest version and does not mention Apple with the version details, then you are all set.

If however you still see apple version, then type the following two lines, which will manually set our path to the local git distro instead of the Apple one.

export PATH=/usr/local/bin:$PATH
git --version

IF YOU DON'T HAVE HOMEBREW, FOLLOW THESE STEPS

Check version

$ git --version

Backup (or remove) Apple git (Optional)

$ sudo mv /usr/bin/git /usr/bin/git-apple

Install Homebrew if you didn’t have

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

Or update if you already have

$ brew update && brew upgrade

Install Git with Homebrew

$ brew install git

Symbolic link

$ brew link --force git

Quit terminal and open a new terminal, then check version.

$ git --version

You should see…

git version <latest version>

Nice! We’re safe now! And next time you can just…

$ brew update && brew upgrade

jaquinocode
  • 634
  • 7
  • 12
kvm006
  • 4,554
  • 2
  • 18
  • 22
  • 34
    Regarding `sudo mv /usr/bin/git /usr/bin/git-apple`: Renaming system git will not work on macOS El Capitan and later unless you disable System Integrity Protection as described here https://apple.stackexchange.com/a/248229/16290. Personally I just update my `PATH` so that `/usr/local/bin` comes before `/usr/bin`. That way Homebrew's git always takes precedence (after you close terminal and re-open). For git security vulnerabilities, Apple will eventually update bundled git with their security updates. In the meantime one can simply avoid using the built-in git by configuring PATH as described. – Nate Cook May 31 '18 at 15:43
  • 3
    I didn't have to do the path export at all — brew handled it all automatically – Ronak Shah Aug 16 '20 at 03:08
  • 5
    I actually had to `brew upgrade git` because I already had `git` installed – mfaani Jan 22 '21 at 15:21
  • 2
    export PATH=/usr/local/bin:$PATH worked for me. Thanks. – Nick Van Brunt Mar 16 '21 at 14:08
  • thank you @NateCook. Finally got precisely what I was looking for. – Khushhal Apr 11 '21 at 08:20
  • to override system git simply add `alias git="/usr/local/bin/git"` to your `.zshrc` or `.bashrc` or which ever shell you prefer... @NateCook, good idea but putting `/usr/local/bin` in front of `/usr/bin` on the path alters too much for me. – Timothy L.J. Stewart Sep 12 '21 at 04:34
  • 2
    It worked for me with a little modification. I have Homebrew installed already but I had the apple version of git linked in /usr/local/bin (`cd /usr/local/bin && ls -la => git -> ../git/bin/git`. So I did it `brew link --overwrite git` and it fixed the probem (`cd /usr/local/bin && ls -la => git -> ../Cellar/git/2.35.1/bin/git`) ; `git --version => OK latest from brew` – Céline de Roland Feb 02 '22 at 06:53
  • 1
    "IF YOU DON'T HAVE HOMEBREW, FOLLOW THESE STEPS... Install Homebrew if you didn’t have" thanks – crumbug Feb 22 '22 at 15:51
  • If you still see an old git version after running "brew install git", the new version is not linked. Run "brew link git" to link it. – ksalgado Apr 19 '22 at 04:44
  • thanks buddy you made my day. It works fine on my mac i didn't had brew install so i have install it 1st and then there was little confusion i had git installed but old version i tried to update it was rejecting but brew link --overwrite git does the trick now i have the latest git on my mac. – Adam Noor Jun 23 '22 at 14:40
104

if using homebrew you can update sim links using

brew link --overwrite git
rafaecheve
  • 1,226
  • 1
  • 9
  • 4
  • 12
    I tried all of the other answers above this one and this is what finally set me up with the latest version of git. running `brew install git` got me the latest version but it didn't set up the symlink properly. running `brew link git` gave me an error saying it couldn't symlink git because it already existed. finally running `brew link --overwrite git` put the nail in the coffin so now `git --version` shows i'm on the most up to date version of git – Gharbad The Weak May 05 '20 at 18:08
  • 2
    Works perfect! Other solutions install new versions but doesn't update when i check from `git --version` – Taady Sep 28 '21 at 11:59
  • 4
    For whatever reason, I had to close/reopen my terminal in order to realize the change. The command worked fine, just not in the existing terminal. – qkerby Feb 22 '22 at 14:50
  • Should be the accepted answer. Just plain config. – Julián Chamalé May 09 '23 at 17:15
60

It would probably be better if you added:

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

to a file named .bashrc in your home folder. This way any other software that you might install in /usr/local/git/bin will also be found first.

For an easy way to do this just type:

echo "export PATH=/usr/local/git/bin:/usr/local/sbin:$PATH" >> ~/.bashrc

into the Terminal and it will do it for you.

NissimL
  • 148
  • 8
kmikael
  • 4,962
  • 3
  • 32
  • 34
  • 1
    Like the README.txt says, git is installed to /usr/local/git. – bananaaus Mar 09 '12 at 05:06
  • 19
    kmikael's handy tip is just for bash. (To change mac default shell unlock the accounts or "users & groups" window in system prefs, control-click account for "advanced options".) A suggestion using bash_profile instead of bashrc, e.g. `echo 'export PATH="/usr/local/git/bin:/usr/local/sbin:~/bin:$PATH"' >> ~/.bash_profile` is at [moncefbelyamani.com](http://www.moncefbelyamani.com/how-to-install-xcode-homebrew-git-rvm-ruby-on-mac/). Discussion of whether to add to .bash_profile or .bashrc is at [superuser.com](http://superuser.com/questions/409186/environment-variables-in-bash-profile-or-bashrc) – miker Dec 03 '12 at 16:50
  • What did you use to install `git`? I just checked the latest version, the official installer [here](http://git-scm.com/downloads) installs git into `/usr/local/git/bin/git`, which means the above method should still work on 10.8.4. – kmikael Jun 25 '13 at 09:16
  • Thanks @miker it worked for me with .bash_profile, but not .bashrc. – adimauro Mar 29 '14 at 11:01
  • I unchecked this solution, even if it works. Please read my update of my message for the explanation. – Nicolas Renon Oct 02 '14 at 09:05
50

For me, with Homebrew 1.6.7, the following worked:

brew upgrade git
Karolis.sh
  • 1,618
  • 20
  • 20
  • 2
    As of today with a macOS Cataline (10.15.6), this one line of code works. To verify the version, just restart your terminal. It just overrides the Apple version of git. – Enis Arik Aug 18 '20 at 19:31
23

I used the following to upgrade git on mac.

hansi$ brew install git 

hansi$ git --version 
git version 2.19.0


hansi$ brew install git
Warning: git 2.25.1 is already installed, it's just not linked
You can use `brew link git` to link this version.

hansi$ brew link git 
Linking /usr/local/Cellar/git/2.25.1... 
Error: Could not symlink bin/git
Target /usr/local/bin/git
already exists. You may want to remove it:
  rm '/usr/local/bin/git'

To force the link and overwrite all conflicting files:
  brew link --overwrite git

To list all files that would be deleted:
  brew link --overwrite --dry-run git

hansi$ brew link --overwrite git 
Linking /usr/local/Cellar/git/2.25.1... 205 symlinks created


hansi$ git --version
git version 2.25.1
Janesh Kodikara
  • 1,670
  • 1
  • 12
  • 23
  • 2
    in my case i had git already installed, and i installed again using homebrew which downloaded a newer version, but it wasn't linked properly. I ended up needed to manually unlink, then relink then it worked. I also needed to reopen my terminal session so that it would find the new version instead of the older/system version: `brew unlink git && brew link git && source ~/.bashrc` – FireDragon Jul 09 '20 at 09:40
  • It was possible to upgrade Git from 2.25.1 to 2.29.2 with the upgrade command ( brew upgrade git ) – Janesh Kodikara Nov 02 '20 at 02:53
17

After searching for "trouble upgrading git on mac" on Google, I read several posts and attempted the following before resolving the problem by completing step 4:

  1. I updated my terminal path by using the above mention export command. Every time I quit the terminal and restarted it, when I typed git --version the terminal, it still return the older version 1.8.

  2. I followed the README.txt instructions for upgrading to the current version 2.0.1 that comes with the .dmg installer and when I restarted the terminal, still no go.

  3. I looked for /etc/path/ folder as instructed above and the directory called "path" does not exist on my Mac. I am running OS X Mavericks version 10.9.4.

  4. Then I recalled I have Homebrew installed on my Mac and ran the following:

    brew --version
    brew update
    brew search git
    brew install git
    

This finally resolved my problem. If anyone has some insight as to why this worked, further insight would be greatly appreciated. I probably have some left over path settings on my system from working with Ruby last year.

Shaun English
  • 171
  • 1
  • 6
  • 2
    I had already installed git. So when following last step `brew install git` I got an error message and it suggested to execute the `brew upgrade git` command instead. You are the man! That did simply the trick :) – Pathros Aug 28 '19 at 02:21
17

@rafaecheve's response worked perfectly in my case as a Mac user. I had a couple versions of git installed on my machine but using the package manager, Homebrew, and the following code:

brew link --overwrite git

I received this confirmation

Linking /usr/local/Cellar/git/2.32.0... 212 symlinks created.

Voila!

DataSci-IOPsy
  • 308
  • 2
  • 11
15

I recently upgraded the Git on my OS X machine to the latest also. I didn't use the same .dmg you used, but when I installed it the binaries were placed in /usr/local/bin. Now, the way my PATH was arranged, the directory /usr/bin appears before /usr/local/bin. So what I did was:

cd /usr/bin
mkdir git.ORIG
mv git* git.ORIG/

This moves the several original programs named git* to a new subdirectory that keeps them out of the way. After that, which git shows that the one in /usr/local/bin is found.

Modify the above procedure as necessary to fit wherever you installed the new binaries.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
7

The installer from the git homepage installs into /usr/local/git by default. However, if you install XCode4, it will install a git version in /usr/bin. To ensure you can easily upgrade from the website and use the latest git version, edit either your profile information to place /usr/local/git/bin before /usr/bin in the $PATH or edit /etc/paths and insert /usr/local/git/bin as the first entry.

It may help to someone at-least changing the order in /etc/paths worked for me.

Mohammad Arif
  • 6,987
  • 4
  • 40
  • 42
5

the simplest way I found so far is from git official website. It just computed dependencies and downloaded all of the required libraries/tools

http://git-scm.com/book/en/Getting-Started-Installing-Git

The other major way is to install Git via MacPorts (http://www.macports.org). If you have MacPorts installed, install Git via

$ sudo port install git-core +svn +doc +bash_completion +gitweb

Kiddo
  • 5,052
  • 6
  • 47
  • 69
5

On macOS Monterey homebrew puts the binaries at /opt/homebrew/bin

Adding this to the .bash_profile will precedence homebrew git over apple-git

export PATH=/opt/homebrew/bin:$PATH
M. Reza Nasirloo
  • 16,434
  • 2
  • 29
  • 41
3

Without Homebrew

  1. Use the installer from git's website.
  2. Update your ~/.bash_profile file. Notice this command differs from kmikael's answer by what it puts in the file:
    • Other command: export PATH=/usr/local/git/bin:/usr/local/sbin/:[and so on]
    • Below command: export PATH="/usr/local/git/bin:/usr/local/sbin:$PATH"
    • Use whichever one you prefer.

echo 'export PATH="/usr/local/git/bin:/usr/local/sbin:$PATH"' >> ~/.bash_profile

  1. If you're using Xcode, you'll need to add some symbolic links.
    • Example: ln -s /opt/local/bin/git /usr/bin/git
  2. Restart terminal.
    • which git should say the directory in the README.txt file from the dmg.
    • git --version should say the updated version.
    • echo $PATH should start with /usr/local/git/bin:/usr/local/sbin:
Katrina
  • 1,922
  • 2
  • 24
  • 42
2

In order to keep both versions, I just changed the value of PATH environment variable by putting the new version's git path "/usr/local/git/bin/" at the beginning, it forces to use the newest version:

$ echo $PATH

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/git/bin/

$ git --version

git version 2.4.9 (Apple Git-60)

original value: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/git/bin/

new value: /usr/local/git/bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin

$ export PATH=/usr/local/git/bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin

$ git --version

git version 2.13.0

Community
  • 1
  • 1
1

The latest version was not available as a binary for mac on git-scm.com, so I installed from source. I was missing a required package for localization, and added NO_GETTEXT=true to install without localization.

git clone https://github.com/git/git.git
cd git
make NO_GETTEXT=true
make NO_GETTEXT=true install

This installed git under ~/bin which I then had to add to the beginning of my PATH variable.

Tmarty
  • 477
  • 5
  • 10
  • This works fine and compiles on macOS 10.10! But now I've realised that I really also need to upgrade `curl`, which is stuck on 7.43.0 when my linuxbox has 7.68.0. Off to compile curl... – Tomachi Sep 22 '21 at 20:10
0

I prefer not to alter the path hierarchy, but instead deal with git specifically...knowing that I'm never going to use old git to do what new git will now manage. This is a brute force solution.

NOTE: I installed XCode on Yosemite (10.10.2) clean first.

I then installed from the binary available on git-scm.com.

$ which git
/usr/bin/git
$ cd /usr/bin
$ sudo ln -sf /usr/local/git/bin/git
$ sudo ln -sf /usr/local/git/bin/git-credential-osxkeychain
$ sudo ln -sf /usr/local/git/bin/git-cvsserver
$ sudo ln -sf /usr/local/git/bin/git-receive-pack
$ sudo ln -sf /usr/local/git/bin/git-shell
$ sudo ln -sf /usr/local/git/bin/git-upload-archive
$ sudo ln -sf /usr/local/git/bin/git-upload-pack
$ ls -la
(you should see your new symlinks)
4Z4T4R
  • 2,340
  • 2
  • 26
  • 45
0

I did it in this way:

  1. Open GitHub application installed on Mac
  2. Click on Advanced tab → Install command line tools
  3. Once you get a message that all commands have been installed close your terminal and reopen it.
  4. Now check git --version, it should give you the latest version.
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Waaheeda
  • 394
  • 3
  • 14
0

You need to adjust shell path , the path will be set in either .bashrc or .bash_profile in your home directory, more likely .bash_profile.

So add into the path similar to the below and keep what you already have in the path, each segment is separated by a colon:

export PATH="/usr/local/bin:/usr/bin/git:/usr/bin:/usr/local/sbin:$PATH"
Viktor
  • 2,623
  • 3
  • 19
  • 28
Windy71
  • 851
  • 1
  • 9
  • 30
-1

You can do this without any commands. The Git installer mentioned by Katrina is the solution, but it's easier to do this:

  1. To enter the link - https://sourceforge.net/projects/git-osx-installer/

  2. Under "Project Activity" select the latest version, download and open the file with the extension .pkg.

  3. Then, in the Security & Privacy system preferences, select the "Open anyway" option. After installation, you will have the latest version of the git

Had3r
  • 249
  • 3
  • 4