【发布时间】:2015-12-05 07:30:45
【问题描述】:
我想通过 macports 而不是 Apple Git 使用最新版本的 Git。但是当我在终端应用程序上输入git --version 时,它会显示Apple Git 版本。
你能告诉我如何在 Mac 上使用最新版本的 git 吗?
【问题讨论】:
我想通过 macports 而不是 Apple Git 使用最新版本的 Git。但是当我在终端应用程序上输入git --version 时,它会显示Apple Git 版本。
你能告诉我如何在 Mac 上使用最新版本的 git 吗?
【问题讨论】:
我假设您已经安装了 xCode 和 macports 并且工作正常。为了使用 macports git 首先你必须安装它:
sudo port install git
然后将此行添加到您的主目录中的 .profile 文件中:
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
echo "export PATH=/opt/local/bin:/opt/local/sbin:$PATH" >> $HOME/.profile" 之类的东西应该这样做。
作为最后一步,您必须 source .profile 或 .bash_profile 使这些更改生效,如下所示:source $HOME/.profile。
总结一下,在 MacOSX 上打开终端并输入:
$ sudo port install git
$ echo "export PATH=/opt/local/bin:/opt/local/sbin:$PATH" >> $HOME/.profile
$ source .profile
$ which git
/opt/local/bin/git
每次加载 shell 时,MacPorts 路径都应该可用。如果这不起作用,请尝试使用 .bash_profile 或 .bashrc 保存环境 $PATH 变量。
【讨论】:
现在是 2022 年,我的 Mac 正在使用 zsh。我想使用 MacPorts 将 git 更新到最新版本,而不是使用 Apple Git。我不使用 Homebrew。
% git --version
git version 2.30.1 (Apple Git-130)
%which git
/usr/bin/git
% port contents git
Port git is not installed.
很明显,我的电脑正在使用 Apple Git。 我需要更新 MacPorts 并安装 git
% sudo port selfupdate
// macports updates
% sudo port install git
// macports installs git
% port contents git
// now lots of files get listed
不过,此时我的电脑仍在使用 Apple Git。
% git --version
git version 2.30.1 (Apple Git-130)
%which git
/usr/bin/git
所以现在我只需要通过为 MacPorts git 添加一个新的 PATH 来更新我的“.zshrc”文件。 MacPorts 将东西放在“/opt/local/bin”中,所以我们需要让我们的 shell 知道这一点。 使用 vim(或 vi 或 pico)编辑 .zshrc
% vim ~/.zshrc
打开编辑器,添加这些行(在某个地方,找到一个好位置[确保没有其他与 Git 相关的 PATH 添加])
# PATH for MacPorts git install
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
保存文件。
此时,我建议重新启动您的控制台会话(您不需要重新启动机器)。或者,您可以在控制台中键入此行...
source .zshrc
此时,如果一切顺利,您现在应该使用最新的 Git。
% git --version
git version 2.35.3
% which git
/opt/local/bin/git
祝你好运!
【讨论】: