警告:如何将git添加到PATH中?

19

我在 OS X El Capitan 上安装了 Homebrew。在安装的最后一步中,我应该使用以下命令安装 git:

brew install git

这还好。现在,如果我运行“brew doctor”,就会出现以下情况:

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!

Warning: Git could not be found in your PATH.
Homebrew uses Git for several internal functions, and some formulae use Git
checkouts instead of stable tarballs. You may want to install Git:
  brew install git

如果我再次运行'brew install git',将会得到以下结果:

$ brew install git 
Warning: git-2.7.4 already installed

毋庸置疑,我的Git版本是2.7.4。执行'brew update'命令出现以下错误:

$ brew update
Warning: git-2.7.4 already installed
Error: Git must be installed and in your PATH!

问题:如何修复我的 PATH,以便于能够在 PATH 中找到 git,并使 'brew doctor' 的结果为 'Your system is ready to brew'?


附加信息:

  • which -a git results in:

    $ which -a git
    /usr/local/bin/git
    /usr/local/bin/git
    /usr/bin/git
    
  • I tried a lot of potential fixes, meanwhile I executed:

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

    My ~/.bash_profile now looks like this:

    # Setting PATH for Python 2.7
    # The orginal version is saved in .bash_profile.pysave
    PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
    export PATH=/usr/local/bin:$PATH
    
  • When I try to install cask:

    $ brew tap caskroom/cask
    ==> Installing git
    Warning: git-2.7.4 already installed
    Error: Git is unavailable
    
  • brew config:

    $ brew config
    HOMEBREW_VERSION: 0.9.5
    ORIGIN: (none)
    HEAD: (none)
    Last commit: never
    HOMEBREW_PREFIX: /usr/local
    HOMEBREW_REPOSITORY: /usr/local
    HOMEBREW_CELLAR: /usr/local/Cellar
    HOMEBREW_BOTTLE_DOMAIN: https://homebrew.bintray.com
    CPU: quad-core 64-bit haswell
    OS X: 10.11.4-x86_64
    Xcode: 7.3
    CLT: 7.3.0.0.1.1457485338
    Clang: 7.3 build 703
    X11: N/A
    System Ruby: 2.0.0-p648
    Perl: /usr/bin/perl
    Python: /usr/local/bin/python => /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin/python2.7
    Ruby: /usr/bin/ruby => /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
    Java: 1.8.0_40
    

编辑:添加了cask到附加信息。

编辑2:添加了brew config并添加了brew doctor前缀。


请同时发布 brew doctorbrew config 的完整输出。 - bfontaine
@bfontaine 我添加了 brew config 并完成了 brew doctor(关于 Homebrew 警告的一般注释,我一开始认为它们不重要)。 - ncw
谢谢;执行 source ~/.bash_profile 命令可以解决这个问题吗? - bfontaine
很遗憾,不行。我两天前尝试过TheGeorgeous的方法,但是没用。我刚刚也尝试了一下,brew doctor仍然提供相同的输出。 - ncw
brew info git 的输出是什么? - bfontaine
我已经修复了,可以看看我的采纳答案。感谢您的帮助@bfontaine! - ncw
7个回答

32

首先,在终端中将目录更改为/usr/local/Homebrew(对于旧版本的 MacOS,请使用cd cd /usr/local/Library/Homebrew)。 然后运行:

git pull origin master
brew update && brew upgrade

16
适用于较新版本的MacOS,请使用cd /usr/local/Homebrew路径。 - sajad Ayooby

14

对我来说,这是由于缺少xcode工具所致,通过 xcode-select --install 命令解决。

其他几个建议的修复方案,包括在brew目录下执行git fetch命令和/或更改git的core.autocrlf,都不能解决这个问题。

详细地说,我从brew中卸载了git(使用命令brew uninstall --force git),但是苹果自己的git就无法工作了(/usr/bin/git --version显示missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun)。

运行brew doctor后建议运行xcode-select --install,我运行了这个命令,解决了苹果自带的git问题,并且也修复了brew。(然后可以通过brew重新安装git,使用命令brew install git)。


1
我刚刚升级到macOS Mojave,我不得不重新安装Xcode命令行工具才能再次使用brew(和git)。 - Chris
这个方法对我来说可行,但是为什么“答案”要手动进行git pull操作呢?使用包管理器的整个精髓就在于不需要手动操作,即使是Homebrew也是如此。唉,唉。 - jumping_monkey

1
如果您和我一样,使用的是macOS El Capitan系统,并且没有/usr/local/Library/ENV/scm/git文件夹,那么您可以采取以下措施。
cd /usr/local/
# The folder where you've installed Homebrew
git config --list --local

查找: core.autoxrlf=false

如果没有看到,请运行: git config --local core.autocrlf false

然后运行:

git fetch origin
git reset --hard origin/master
brew update

1

只需运行:

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

你的PATH变量应该看起来像这样。
PATH="/usr/bin:/usr/local/bin"

很遗憾没有起作用;我运行了命令 export PATH=/usr/local/bin:$PATH,但这并没有改变我的 ~/.bash_profile。之后我手动更改了 ~/.bash_profile,使其看起来像这样:PATH="/usr/bin:/usr/local/bin" export PATH=/usr/local/bin:$PATH如果我完全误解了你的回答,请温柔点。我对Unix终端没有太多经验。 - ncw
你检查过你的.bashrc了吗?另外,你需要运行source .bashrc来重新加载你的配置。 - TheGeorgeous
如果我运行 source .bashrc,它会显示 没有这个文件或目录。我只需关闭并重新打开终端以重新加载配置。据我所知,在这种情况下,我只需要查找 .bash_profile 并正确设置 PATH,这样 brew 就可以找到 git 了? - ncw

1
我找到了解决问题的方法。它是由于/usr/local/Library/ENV/scm/git文件格式错误引起的:
$ /usr/local/Library/ENV/scm/git --version
-bash: /usr/local/Library/ENV/scm/git: /bin/sh^M: bad interpreter: No such file or directory

所以我编辑了文件格式:
$ vi /usr/local/Library/ENV/scm/git

按下“ESC”键,输入::set fileformat=unix,按下“Enter”键,再输入::wq!。这对我解决了问题。

0

当我尝试运行brew update时,遇到了相同的症状。

在我的情况下,问题是由于设置了一个bash变量GIT(指向一个目录)所导致的。因此,解决方案是在brew命令中取消设置它。

$ GIT= brew update

0

很可能是与libintl.8.dylib有关的问题,就像许多其他类似的问题一样:

locate libintl.8.dylib

如果/gettext//libintl.*.dylib存在

brew link -f gettext

前往(根据版本)

cd /usr/local/Homebrew 
cd /usr/local/Library/Homebrew 

并执行(确保您在.../Homebrew目录中)

git pull origin master
rm -fr ".git/rebase-apply"
brew update && brew upgrade

这个最后花了一些时间,但之后一切应该都能正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接