Brew更新失败:未跟踪的工作树文件将被合并覆盖。

208

尝试使用 brew update 更新Homebrew时,我收到了以下错误信息。

error: The following untracked working tree files would be overwrittenby merge:     
 Library/Formula/argp-standalone.rb
 Library/Formula/cocot.rb 
Please move or remove them before you can merge. 
Aborting 
Updating e088818..5909e2c 
Error: Failed while executing git pull  origin refs/heads/master:refs/remotes/origin/master

我发现有个博客文章讲述了一个人在安装Mountain Lion(我这周也做了)后遇到类似问题的经历。他解释了如何删除错误信息所指的文件。

I removed these files:
$ cd $(brew --prefix)
$ rm cocot.rb
However, removing these files didn't help with the brew update. Instead I had to manually update brew through git:
$ cd $(brew --prefix)
$ git fetch origin
$ git reset --hard origin/master
$ brew update
Already up-to-date.

假设这些指令是正确的(也许我不应该这样假设),我尝试按照这些指令并执行。
    $ cd $(brew --prefix)
    $ rm cocot.rb

然而,当我尝试删除cocot.rb时,它显示“文件不存在”。

有一件事我不确定的是cd $(brew --prefix)。这些是我要键入的确切单词还是我需要用什么替换prefix?cd成功了,所以我认为它是正确的——它将我移动到了/usr/local,但没有文件可供删除。/usr/local的内容如下:

Cellar              clamXav             git             mysql               var Library             doc             heroku              mysql-5.5.15-osx10.6-x86_64 README.md           etc             include             rvm bin             foreman             lib             share

无论如何,你知道我如何修复“brew update”吗?
更新:按照favoretti的说明删除文件并尝试chown -R <your_username> $(brew --prefix)/.git后,运行brew update时出现以下错误。
error: The following untracked working tree files would be overwritten by merge:
    Library/Aliases/gperftools
    Library/Aliases/hashdeep
    Library/Aliases/htop
    Library/Aliases/nodejs
    Library/Aliases/ocio
    Library/Aliases/oiio
    Library/Aliases/pgrep
    Library/Aliases/pkill
    Library/Aliases/qt4
    Library/Aliases/twolame
    Library/Aliases/wxwidgets
    Library/Contributions/cmds/brew-aspell-dictionaries
    Library/Contributions/cmds/brew-beer.rb
    Library/Contributions/cmds/brew-dirty.rb
    Library/Contributions/cmds/brew-graph
    Library/Contributions/cmds/brew-grep
    Library/Contributions/cmds/brew-leaves.rb
    Library/Contributions/cmds/brew-linkapps.rb
    Library/Contributions/cmds/brew-ls-taps.rb
    Library/Contributions/cmds/brew-man
    Library/Contributions/cmds/brew-md5-to-sha1
    Library/Contributions/cmds/brew-mirror-check.rb
    Library/Contributions/cmds/brew-pull.rb
    Library/Contributions/cmds/brew-readall.rb
    Library/Contributions/cmds/brew-server
    Library/Contributions/cmds/brew-services.rb
    Library/Contributions/cmds/brew-switch.rb
    Library/Contributions/cmds/brew-test-bot.rb
    Library/Contributions/cmds/brew-tests.rb
    Library/Contributions/cmds/brew-unpack.rb
    Library/Contributions/cmds/brew-which.rb
    Library/Contributions/cmds/git
    Library/Contributions/cmds/svn
    Library/ENV/4.3/apr-1-config
    Library/ENV/4.3/bsdmake
    Library/ENV/4.3/c++
    Library/ENV/4.3/c89
    Library/ENV/4.3/c99
    Library/ENV/4.3/cc
    Library/ENV/4.3/clang
    Library/ENV/4.3/clang++
    Library/ENV/4.3/cpp
    Library/ENV/4.3/g++
    Library/ENV/4.3/gcc
    Library/ENV/4.3/git
    Library/ENV/4.3/i686-apple-darwin11-llvm-g++-4.2
    Library/ENV/4.3/i686-apple-darwin11-llvm-gcc-4.2
    Library/ENV/4.3/ld
    Library/ENV/4.3/llvm-g++
    Library/ENV/4.3/llvm-g++-4.2
    Library/ENV/4.3/llvm-gcc
    Library/ENV/4.3/llvm-gcc-4.2
    Library/ENV/4.3/make
    Library/ENV/4.3/mig
    Library/ENV/4.3/sed
    Library/ENV/4.3/svn
    Library/ENV/4.3/xcrun
    Library/ENV/libsuperenv.rb
    Library/ENV/pkgconfig/leopard/libcrypto.pc
    Library/ENV/pkgconfig/leopard/libcurl.pc
    Library/ENV/pkgconfig/mountain_lion/libcurl.pc
    Library/ENV/pkgconfig/mountain_lion/libexslt.pc
    Library/ENV/pkgconfig/mountain_lion/libxml-2.0.pc
    Library/ENV/pkgconfig/mountain_lion/libxslt.pc
    Library/Formula/abcl.rb
    Library/Formula/abcmidi.rb
    Library/Formula/aiccu.rb
    Library/Formula/akka.rb
    Library/Formula/alac.rb
    Library/Formula/alure.rb
    Library/Formula/appledoc.rb
    Library/Formula/arangodb.rb
    Library/Formula/argp-standalone.rb
    Library/Formula/argtable.rb
    Library/Formula/autopano-sift-c.rb
    Library/Formula/avian.rb
    Library/Formula/avidemux.rb
    Library/Formula/avro-cpp.rb
    Library/Formula/aws-cloudsearch.rb
    Library/Formula/aws-sns-cli.rb
    Library/Formula/backupninja.rb
    Library/Formula/bact.rb
    Library/Formula/bam.rb
    Library/Formula/basex.rb
    Library/Formula/berkeley-db4.rb
    Library/Formula/bind.rb
    Library/Formula/blazeblogger.rb
    Library/Formula/bochs.rb
    Library/Formula/boost149.rb
    Library/Formula/bsdconv.rb
    Library/Formula/bsdmake.rb
    Library/Formula/buildapp.rb
    Library/Formula/bup.rb
    Library/Formula/byacc.rb
    Library/Formula/cadubi.rb
    Library/Formula/camellia.rb
    Library/Formula/casperjs.rb
    Library/Formula/ccextractor.rb
    Library/Formula/cconv.rb
    Library/Formula/cdo.rb
    Library/Formula/cdpr.rb
    Library/Formula/cgvg.rb
    Library/Formula/checkstyle.rb
    Library/Formula/chordii.rb
    Library/Formula/chruby.rb
    Library/Formula/cifer.rb
    Library/Formula/clhep.rb
    Library/Formula/cntlm.rb
    Library/Formula/cocot.rb
    Library/Formula/cogl.rb
    Library/Formula/collada-dom.rb
    Library/Formula/crash.rb
    Library/Formula/crossroads.rb
    Library/Formula/crosstool-ng.rb
    Library/Formula/css-crush.rb
    Library/Formula/csync.rb
    Library/Formula/ctemplate.rb
    Library/Formula/curlftpfs.rb
    Library/Formula/cutter.rb
    Library/Formula/cvsutils.rb
    Library/Formula/darkstat.rb
    Library/Formula/darner.rb
    Library/Formula/dart.rb
    Library/Formula/dasm.rb
    Library/Formula/debianutils.rb
    Library/Formula/dfc.rb
    Library/Formula/dgtal.rb
    Library/Formula/dhcping.rb
    Library/Formula/di.rb
    Library/Formula/dmtx-utils.rb
    Library/Formula/drip.rb
    Library/Formula/dsniff.rb
    Library/Fo
Aborting
Updating e088818..c1fbc29
Error: Failed while executing git pull  origin refs/heads/master:refs/remotes/origin/master

https://dev59.com/HWgv5IYBdhLWcg3wNuN5 - Dan K.K.
请查看Homebrew Core的官方README.md文件。不需要像一些旧答案建议的那样调整权限。还要检查您是否指向当前的官方源URL。更多信息请参见此答案:https://dev59.com/6GYq5IYBdhLWcg3w5Ui8#40599893?s=2|2.2654#40599893 - Kay V
12个回答

359

您需要执行以下操作:

cd $(brew --prefix)
rm Library/Formula/argp-standalone.rb
rm Library/Formula/cocot.rb

然后执行

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

基本上,再解释一下:

cd $(brew --prefix)

告诉cd将目录更改为brew --prefix将输出的任何内容。如果您尝试执行brew --prefix命令,您应该看到以下类似内容:

brew --prefix
/usr/local
因此,在这种情况下,该命令相当于cd /usr/local
更新的brew版本在其安装前缀和Library/Formula/下具有公式,因此您需要在其中查找那些已过时/更改的文件。
请注意,如果您出于特定原因(如固定版本)自己更改了这些公式,则此操作将将它们恢复为默认值,并可能产生不良影响。
@TedPennings在评论中指出,这对他起作用,但他的顺序是:
1.将所有权转移到my_username:admin,即sudo chown -R ted:admin $(brew --prefix) 2.运行上述两个git命令git fetch origingit reset --hard origin/master

1
当我执行 git reset --hard origin/master 时,它似乎只部分起作用了。这是输出结果。error: unable to unlink old '.gitignore' (Permission denied) error: unable to create file CONTRIBUTING.md (Permission denied) Checking out files: 100% (2493/2493), done. fatal: Could not reset index file to revision 'origin/master'. - BrainLikeADullPencil
3
这意味着您的权限出了问题。一种方法是运行 sudo git reset --hard,另一种更好的方法是修复权限并执行 chown -R <your_username> $(brew --prefix)/.git - favoretti
19
这个解决方案对我非常有用,但我必须将所有内容的拥有者更改为我的用户名:admin。具体操作是,运行以下命令:sudo chown -R ted:admin $(brew --prefix) ,然后执行上述两个git命令:git fetch origingit reset --hard origin/master - Ted Pennings
2
@TedPennings:太酷了!我会将你的命令作为可能的额外步骤添加到答案中。 - favoretti
值得注意的是,您可能不需要手动更改权限和移动目录。例如,请参见下面的答案(https://dev59.com/6GYq5IYBdhLWcg3w5Ui8#40599893)。 - Kay V
显示剩余7条评论

280
cd $(brew --prefix)
git reset --hard HEAD
brew update

遇到了与原问题类似但涉及不同冲突文件的问题。只需重置即可让我更新并解决问题。 - David
17
你可能也想执行 "git add ." 然后执行 "git stash" :P - mkk
13
如果你使用 cd $(brew --prefix) 命令切换到 Homebrew 目录,并且通过 git reset --hard HEAD 命令重置了 Git 状态,但是执行 git status 命令时显示工作目录不干净的话,你可能还需要运行 git clean -df 命令来清理状态。请注意,这些修改不会改变原有意思。 - Strand McCutchen
82
我没有使用 git reset --hard HEAD(这对我无效),而是使用了 git fetch --allgit reset --hard origin/master - Kevin.
我确实需要使用sudo命令来执行git reset --hard HEAD,但最终它起作用了,谢谢! - Windgazer
显示剩余5条评论

46

这是由于早期的update代码存在一个已经修复的老漏洞造成的。然而,该漏洞的性质要求您执行以下操作:

cd $(brew --repository)
git reset --hard FETCH_HEAD

如果brew doctor仍然抱怨未提交的修改,请运行以下命令:

如果brew doctor 仍然抱怨有未提交的修改,请运行此命令:

cd $(brew --repository)    
git clean -fd

3
当被接受的答案无法解决问题时,这个解决方案对我很有效。 - JBCP

31
$ cd $(brew --prefix)
$ git clean -fd
$ git reset --hard
$ sudo chown -R `whoami` `brew --prefix`
$ brew update

注意:步骤2&3对我有效,因为我在出现错误之前执行了步骤5然后再执行了步骤4。在更改文件夹所有者之前执行brew update导致了整个问题。


这对我有用。我想补充一下,这可能是由其他程序以其他权限在/usr/local中安装东西引起的。对我来说,是MacPGP以root权限安装了自己。 - Overbryd
1
在升级到Mac OS X 10.11后,/usr/local的权限出了问题,这个方法解决了我的问题。谢谢! - CletusW
值得注意的是,您可能不需要手动更改权限和移动目录。例如,请参见https://dev59.com/6GYq5IYBdhLWcg3w5Ui8#40599893。 - Kay V

6

我曾经遇到过类似的问题,我的brew包库被下载为root用户,然后我就无法运行brew update,因为git会抱怨无法合并文件。

解决方法是:

sudo chown -R <user> /usr/local/Library

5
重置brew存储库应该是最有效的方法,如果您不介意放弃可能已应用的配方上的潜在修改:
$ cd `brew --prefix`
$ git fetch origin
$ git reset --hard origin/master
$ brew cleanup --force
$ brew update

4

我能够在我的电脑上解决这个问题,方法是在以下路径添加管理员组的写入权限(以及设置setgid位):

sudo chmod -R g+ws /usr/local /Library/Caches/Homebrew

在执行过程中,s设置了组ID,以便在下次其他管理员更新或升级Homebrew时不会失去写入许可。

当我尝试执行brew cleanup --force时,这个回答有些不完整,因为Homebrew在尝试删除/usr/local/Cellar/中的内容时出现了权限问题。(不幸的是,我目前无法进一步调查此问题。)


2
有一个流行的脚本用于修复Homebrew权限问题,据说效果非常好。我自己没有使用过它,而是手动进行了更改 :) 但这可能会帮助其他人:https://gist.github.com/rpavlik/768518 - Steve Benner

1
sudo chown -R USER /usr/local/bin

注意 - 对于用户,请使用whom命令,即您的用户名

sudo brew doctor

或者概括为:sudo chown -R $USER /usr/local/bin,其中添加 $ 表示 USER 环境变量,该变量保存当前用户名。 - Cometsong

1

另一种选择是仅使用git stash

brew update命令执行git pull,因此如果有任何更改的文件(甚至是文件属性,在我递归更改目录权限时就发生了这种情况),则必须解决冲突。使用git stash是一种方法;它将任何更改放在一边,因此您有效地恢复到上次更新的存储库版本。 Homebrew的“位置”默认为/usr/local,并安装为git存储库。您可以查找.git文件夹以自己查看。我正在编写一个Homebrew卸载脚本,很快会在我的GitHub个人资料中发布,带有更多信息。


1
这种方法可能比其他一些方法更简单。它包括:
  • 修复git问题,以便再次委托管理变更。
  • 不需要手动移动文件或目录。
  • 不需要手动调整文件或目录权限。
步骤(对于希望了解说明的人有注释):
cd $(brew --repository)                              // see Note 1 below
git remote add brew https://github.com/Homebrew/brew // see Note 2 below
git pull brew master                                 // promising fast-forward report!
brew update                                          // see Note 3 below 

概述:
据我所知,这个问题的实际原因是仓库网址的更改。现在是 brew,以前是 brew.git。(完整的最新网址:https://github.com/Homebrew/brew

注1:第一条命令可以将你从文件结构中的任何位置带到正确的目录。对于我来说,目录结构与其他人展示的不同(Mac OS 10.11.16),但使用这个命令,这些差异就不重要了。

注2:第二条命令将正确的远程网址添加到一个新的别名;我这样做只是为了防万一这种方法没有达到我想要的效果,我需要之前的地址。由于新的远程工作正常,我会邀请其他人评论如何简单地更改由 origin 别名引用的网址。我很乐意更新答案以反映适合您的解决方案。

注3:第四条命令正是所需的结果:它报告了大量的更新,包括特别好的报告 "==> Migrated HOMEBREW_REPOSITORY to /usr/local/Homebrew!"(强调他们的)。


当你运行brew update时遇到504错误时,这个方法是有效的。 - Omnilord

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