I have installed the newest version of git (1.8.3) using homebrew but when I type
git --version
in my terminal, it prints:
git version 1.7.10.2 (Apple Git-33)
What should I do to replace the old version of git with the new one?
I have installed the newest version of git (1.8.3) using homebrew but when I type
git --version
in my terminal, it prints:
git version 1.7.10.2 (Apple Git-33)
What should I do to replace the old version of git with the new one?
All the tricks mentioned here in several answers are not necessary anymore on Intel Macs running macOS Sierra or higher with the latest Homebrew. Forget export PATH="..."
and modifications to ~/.bash_profile
.
You simply do
brew install git
and you're done.
To confirm, open a new terminal window/tab and type
git --version
Please don't forget to open a new window. Terminals that were open before you started to install
will not inherit any changes.
brew doctor
it will tell you that your system paths take precedence over the homebrew ones. The output will also include how to fix it. E.g. by running echo export PATH="/opt/homebrew/bin:$PATH" >> .zshrc
– Kerim Güney
Mar 10 '21 at 12:45
/usr/local/bin
seems to be in /etc/paths
now. It for sure does not work for ARM Macs where Homebrew binaries are installed in /opt/homebrew/bin
.
– nohillside
Jun 19 '22 at 10:15
source ~/.zshrc
to refresh environment.
– mindOf_L
Jul 19 '23 at 14:56
If on Intel, add
export PATH="/usr/local/bin:${PATH}"
if on ARM/M1, add
export PATH="/opt/homebrew/bin:${PATH}"
in ~/.bash_profile
followed by
source ~/.bash_profile
solved the problem for my user.
/usr/local/bin
above /usr/bin
in /etc/paths
.
– kevin
Nov 12 '13 at 05:08
brew doctor
to diagnose the issue
– VoxPelli
Dec 04 '13 at 14:22
/etc/paths
. Defining only the "export" may not be enough, if /usr/bin
has "precedence" over /usr/local/bin
(from brew)...
– emgsilva
Dec 09 '13 at 11:43
source ~/.bash_profile
will re-read that. Restarting is just probably faster/easier. :)
–
Oct 05 '15 at 23:12
/usr/local/bin
in PATH
before /usr/bin
make every command find the version of git
which is in /usr/local/bin
before the one which is the standard Apple one in /usr/bin
. And this version is the one installed by Homebrew
.
– dan
May 29 '17 at 10:15
eval "$(/opt/homebrew/bin/brew shellenv)"
Ok, I'm ready to get serious about scm.
$ git --version
git version 1.9.5 (Apple Git-50.3)
Nope, that's not what I wanted. I <3 homebrew, so:
$ brew install git
All set?
$ git --version
git version 1.9.5 (Apple Git-50.3)
Doh! (scratches head)
$ which git
/usr/bin/git
Ah, Apple's git is in /usr/bin, so it trumps the homebrew one. What to do?
(A) Just rename Apple's binary
(B) Let homebrew-managed one take precedence:
[edit PATH export e.g. in ~/.zshrc (oh-my-zsh + iTerm2 FTW! /tangent)]
[specifically: move /usr/local/bin/git: before /usr/bin:]
... and/or (e.g. to more broadly let homebrew stuff trump system installs, and have the precedence apply to all shells and users) also edit /etc/paths file, [moving /usr/local/bin above /usr/bin]
But assuming just the simplest / least invasive approach:
$ sudo mv /usr/bin/git /usr/bin/git-apple
Did it work?
$ which git
/usr/local/bin/git
So far so good, now the moment of truth:
$ git --version
git version 2.2.1
w00t! :) Time to go read http://git-scm.com ! :)
which git
pointing to /usr/local/bin/git
, this didn't work for me. To solve this, I had to uninstall the GitHub Mac app.
– Ben
Dec 22 '15 at 18:28
mv: rename /usr/bin/git to /usr/bin/git-apple: Operation not permitted`
– Vladislav Rastrusny Jun 19 '17 at 13:45brew doctor
. In 2020, it recommended brew link —overwrite git
– ReinstateMonica3167040
Jan 18 '20 at 16:03
/usr/local/bin/git:
before /usr/bin:
. In hindsight, maybe all I needed to do was start a new terminal session!
– samjewell
Feb 16 '21 at 17:02
export PATH="/opt/homebrew/bin:$PATH"
to put homebrew git to the leading position in system PATH.
– Zhou Haibo
Jul 28 '22 at 02:29
Once you've installed the latest git via brew (brew install git
), run this one-liner (as suggested by brew doctor
) if it isn't already there:
echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile
Then quit Terminal an open it again (restart your bash session). You need to do this even if your PATH was already correct, as ZSH and Bash cache the contents of PATH (see the documentation on the built-in command hash
).
That should fix things really fast.
When you use brew install git
for the installation, notice that it install git
under:
==> Summary
/usr/local/Cellar/git/2.29.2: 1,480 files, 39.7MB
You will also get an error, which appears earlier in the log and probably you missed it:
==> Downloading https://homebrew.bintray.com/bottles/git-2.29.2.big_sur.bottle.t
Already downloaded: /Users/chadjinik/Library/Caches/Homebrew/downloads/08165d120fcebc7823c487a6778b2ea0e67fd2cd9177d6e7d656268f474ab5da--git-2.29.2.big_sur.bottle.tar.gz
==> Pouring git-2.29.2.big_sur.bottle.tar.gz
Error: The brew link
step did not complete successfully
The formula built, but is not symlinked into /usr/local
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
Just run:
brew link --overwrite git
and you should be good.
When you type
git --version
in Terminal.app or console, your comment on another question indicated the version it returns is the git in /usr/bin/git
If you installed Xcode 4.5 (and newer), and type
xcrun git --version
in Terminal.app or console, the version it returns is the git in the Xcode app bundle.
If you are using Homebrew to install and update git, the simplest solution is to
rename the original location by renaming it using mv. For example
sudo mv /usr/bin/git /usr/bin/git-ORIGINAL
create a soft link using 'ln -s' to the git binary you installed with Homebrew.
Note that MattDMo has a better solution in the comments.
/usr/bin
, as it's possible that other programs you don't even know about depend on vagaries associated with a particular version. The easiest solution, without moving anything, is to add /usr/local/bin
to your $PATH
environment variable before /usr/bin
- IIRC, homebrew installs to /usr/local/bin
– MattDMo
Jun 03 '13 at 17:03
Install git with brew, the run this.
brew link --force git
Close and reopen terminal to run which git
.
brew link --overwrite git
worked
– wickdninja
Feb 20 '19 at 03:48
Do a simple alias to avoid changing path:
alias git=/usr/local/bin/git
Ok so I ran brew doctor
and I saw this:
Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause formulae that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
git
Of course I run brew link git
. Then I got:
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
I ran brew link --overwrite git
and it worked!
You have to rename the original git by apple in /usr/bin/
to e. g. git-org since /usr/bin
is normally before /usr/local/bin
in your path directory where the brew stuff is.
So:
cd /usr/bin
sudo mv git git-org
and do not forget to link the brew git
brew link git
This assumes that /usr/local/bin
is in your $PATH
environment variable. If you still have problem try to run
brew doctor
and fix the problems mentioned there.
/usr/local/bin
(homebrew's install directory, IIRC) to the $PATH
ahead of /usr/bin
instead of mucking around with renaming and backing up, etc.
– MattDMo
Jun 03 '13 at 17:05
If you are installing git from git-scm.com directly and would want to use the latest downloaded git instead of apple(old) version of git.
/usr/local/bin/git
git --version
, if it returns Apple old version of git then proceed belowcd ~
(change directory to your home directory)vi .bashrc
i
(to insert text in vi editor)export PATH.......
, press enter on top of the export and type the following: export PATH=/usr/local/:$PATH
(Pay extreme caution with PATH
variable do not mess it up else it will cause problems for your OS)
(hopefully new git should be installed in /usr/local/git
):wq
(to save the .bashrc
file)git --version
(you should see new version)I've tried many things including all of this post's answers. Finally I was able to get brew's version of git to run instead of Xcode's by simply deleting Xcode's additional tools folder:
sudo rm -rf /Library/Developer/CommandLineTools
It depends on where your git comes from. Xcode brings a version for example, maybe that is upfront in your path.
Maybe typing
which git
will show where the old one is.
``` brew install git ````
In $HOME/.zshrc you need to add path to make it available
PATH="$(brew --prefix)/opt/git/libexec/git-core:$PATH"
In Terminal reload it
source ~/.zshrc
Test it
which git
– Gedw99 Mar 03 '23 at 12:26/usr/local/opt/git/libexec/git-core/git