如何在 macOS 上将 Git 升级到最新版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8957862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upgrade Git to latest version on macOS?
提问by Nicolas Renon
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
我刚买了一台带有 OS X Lion 的新 Mac,我在终端中检查了默认安装的 git 版本。我得到了答案
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.
我想将 git 升级到最新版本 1.7.8.3,所以我下载了 dmg 安装程序“git-1.7.8.3-intel-universal-snow-leopard.dmg”并启动了它。
After the install, the Terminal still says that the version is 1.7.5.4. What am I doing wrong?
安装后终端还是提示版本是1.7.5.4。我究竟做错了什么?
回答by kmikael
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.
到您的主文件夹中名为 .bashrc 的文件。这样,您可能安装的任何其他软件/usr/local/git/bin
也将首先被找到。
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.
进入终端,它会为你做。
回答by kvm006
It's simple if you already have Homebrew: Assuming you have homebrew installed, type the following:
如果您已经安装了 Homebrew,这很简单:假设您已经安装了Homebrew,请输入以下内容:
brew install git
Once it is installed, then type the following two lines, which will set our path to the local git distro instead of the Apple one.
安装完成后,输入以下两行,这会将我们的路径设置为本地 git 发行版而不是 Apple 发行版。
export PATH=/usr/local/bin:$PATH
git --version
IF YOU DON'T HAVE HOMEBREW, FOLLOW THESE STEPSCheck version
如果您没有 HOMEBREW,请按照以下步骤操作检查版本
$ git --version
$ git --version
Backup (or remove) Apple git (Optional)
备份(或删除)Apple git(可选)
$ sudo mv /usr/bin/git /usr/bin/git-apple
$ sudo mv /usr/bin/git /usr/bin/git-apple
Install Homebrew if you didn't have
如果你没有安装 Homebrew
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Or update if you already have
或者如果你已经有更新
$ brew update && brew upgrade
$ brew update && brew upgrade
Install Git with Homebrew
使用 Homebrew 安装 Git
$ brew install git
$ brew install git
Symbolic link
符号链接
$ brew link --force git
$ brew link --force git
Close Terminal and reopen then check version
关闭终端并重新打开然后检查版本
$ git --version
$ git --version
You should see…
你应该看到…
git version <latest version>
git version <latest version>
Nice! We're safe now! And next time you can just…
好的!我们现在安全了!下次你可以……
$ brew update && brew upgrade
$ brew update && brew upgrade
回答by Shaun English
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:
在 Google 上搜索“在 mac 上升级 git 时遇到问题”后,我阅读了几篇文章并在通过完成第 4 步解决问题之前尝试了以下操作:
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.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.
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.
Then I recalled I have Homebrewinstalled on my Mac and ran the following:
brew --version brew update brew search git brew install git
我使用上面提到的导出命令更新了我的终端路径。每次我退出终端并重新启动它时,当我输入
git --version
终端时,它仍然返回旧版本1.8。我按照 README.txt 说明升级到 .dmg 安装程序附带的当前版本 2.0.1,当我重新启动终端时,仍然不行。
我按照上面的说明查找了 /etc/path/ 文件夹,但我的 Mac 上不存在名为“path”的目录。我正在运行 OS X Mavericks 版本 10.9.4。
然后我回忆起我在 Mac 上安装了Homebrew并运行了以下命令:
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.
这终于解决了我的问题。如果有人对为什么这样做有一些见解,我们将不胜感激。去年使用 Ruby 时,我的系统上可能有一些剩余的路径设置。
回答by Greg Hewgill
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:
我最近也将 OS X 机器上的 Git 升级到了最新版本。我没有使用.dmg
您使用的相同内容,但是当我安装它时,二进制文件被放置在/usr/local/bin
. 现在,按照 my 的PATH
排列方式,目录/usr/bin
出现在/usr/local/bin
. 所以我所做的是:
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.
这会将命名的几个原始程序移动git*
到一个新的子目录,使它们不受干扰。之后,which git
显示/usr/local/bin
找到了一个。
Modify the above procedure as necessary to fit wherever you installed the new binaries.
根据需要修改上述过程以适应新二进制文件的安装位置。
回答by rafaecheve
if using homebrew you can update sim links using
如果使用自制软件,您可以使用更新 sim 链接
brew link --overwrite git
回答by Mohammad Arif
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 $PATHor edit /etc/paths
and insert /usr/local/git/bin
as the first entry.
来自 git 主页的安装程序/usr/local/git
默认安装到。但是,如果您安装XCode4,它将安装一个.git版本/usr/bin
。为确保您可以轻松地从网站升级并使用最新的 git 版本,请编辑您的个人资料信息以放置在$PATH/usr/local/git/bin
之前/usr/bin
,或者编辑并插入作为第一项。/etc/paths
/usr/local/git/bin
It may help to someone at-least changing the order in /etc/pathsworked for me.
至少对更改/etc/paths 中的顺序对我有用的人可能会有所帮助。
回答by Karolis ?arapnickis
For me, with Homebrew 1.6.7
, the following worked:
对我来说,Homebrew 1.6.7
以下工作:
brew upgrade git
回答by Janesh Kodikara
I used the following to upgrade git on mac.
我使用以下内容在 mac 上升级 git。
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
回答by Kiddo
the simplest way I found so far is from git official website. It just computed dependencies and downloaded all of the required libraries/tools
到目前为止我发现的最简单的方法是从 git 官方网站。它只是计算依赖项并下载所有必需的库/工具
http://git-scm.com/book/en/Getting-Started-Installing-Git
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
另一种主要方法是通过 MacPorts ( http://www.macports.org)安装 Git 。如果您安装了 MacPorts,请通过以下方式安装 Git
$ sudo 端口安装 git-core +svn +doc +bash_completion +gitweb
回答by Sandro E Martínez
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:
为了保留两个版本,我只是通过将新版本的git路径“/usr/local/git/bin/”放在开头来更改PATH环境变量的值,它强制使用最新版本:
$ 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)
$回声 $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/git/bin/
$ git --version
git 版本 2.4.9 (Apple Git-60)
original value:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/git/bin/
原始值:/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
新值:/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
$导出路径=/usr/local/git/bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
$ git --version
版本 2.13.0