374

I tried to run brew update and I get an error about my local changes would be lost if I merged. I tried committing my local changes (don't remember making any, but it's been awhile), and that made things worse.

Here's the output:

MBP:Library User$ sudo brew update
error: The following untracked working tree files would be overwritten by merge:
    Library/Aliases/fastcgi
    Library/Aliases/htop
    Library/Aliases/nodejs
    Library/Aliases/ocio
    Library/Aliases/oiio
    Library/Aliases/pgrep
    Library/Aliases/pkill
    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-man
    Library/Contributions/cmds/brew-mirror-check.rb
    Library/Contributions/cmds/brew-missing.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.commit.html.erb
    Library/Contributions/cmds/brew-test-bot.css
    Library/Contributions/cmds/brew-test-bot.index.html.erb
    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/install_homebrew.rb
    Library/Formula/abcl.rb
    Library/Formula/abyss.rb
    Library/Formula/akka.rb
    Library/Formula/apollo.rb
    Library/Formula/appledoc.rb
    Library/Formula/arangodb.rb
    Library/Formula/autoconf.rb
    Library/Formula/automake.rb
    Library/Formula/avidemux.rb
    Library/Formula/bind.rb
    Library/Formula/bsdconv.rb
    Library/Formula/bsdmake.rb
    Library/Formula/camellia.rb
    Library/Formula/cbmbasic.rb
    Library/Formula/cdo.rb
    Library/Formula/checkstyle.rb
    Library/Formula/cifer.rb
    Library/Formula/clhep.rb
    Library/Formula/collada-dom.rb
    Library/Formula/crash.rb
    Library/Formula/crossroads.rb
    Library/Formula/css-crush.rb
    Library/Formula/curlftpfs.rb
    Library/Formula/dart.rb
    Library/Formula/dasm.rb
    Library/Formula/dfc.rb
    Library/Formula/di.rb
    Library/Formula/dsniff.rb
    Library/Formula/dupx.rb
    Library/Formula/dwatch.rb
    Library/Formula/eprover.rb
    Library/Formula/ext2fuse.rb
    Library/Formula/ezlupdate.rb
    Library/Formula/f3.rb
    Library/Formula/fastx_toolkit.rb
    Library/Formula/fceux.rb
    Library/Formula/findbugs.rb
    Library/Formula/freerdp.rb
    Library/Formula/funcoeszz.rb
    Library/Formula/fwknop.rb
    Library/Formula/gabedit.rb
    Library/Formula/gbdfed.rb
    Library/Formula/gconf.rb
    Library/Formula/git-encrypt.rb
    Library/Formula/glm.rb
    Library/Formula/gmap-gsnap.rb
    Library/Formula/gnu-arch.rb
    Library/Formula/gnunet.rb
    Library/Formula/gobby.rb
    Library/Formula/gptfdisk.rb
    Library/Formula/griffon.rb
    Library/Formula/grok.rb
    Library/Formula/gtk-chtheme.rb
    Library/Formula/gtkglextmm.rb
    Library/Formula/gtmess.rb
    Library/Formula/hg-flow.rb
    Library/Formula/hqx.rb
    Library/Formula/htop-osx.rb
    Library/Formula/htpdate.rb
    Library/Formula/imap-uw.rb
    Library/Formula/iozone.rb
    Library/Formula/ipbt.rb
    Library/Formula/ipe.rb
    Library/Formula/ispc.rb
    Library/Formula/ispell.rb
    Library/Formula/jigdo.rb
    Library/Formula/jing.rb
    Library/Formula/jless.rb
    Library/Formula/jpeginfo.rb
    Library/Formula/konoha.rb
    Library/Formula/legit.rb
    Library/Formula/libcouchbase.rb
    Library/Formula/libcuefile.rb
    Library/Formula/libextractor.rb
    Library/Formula/libglademm.rb
    Library/Formula/libgtextutils.rb
    Library/Formula/libinfinity.rb
    Library/Formula/libkate.rb
    Library/Formula/libqalculate.rb
    Library/Formula/libqglviewer.rb
    Library/Formula/libreplaygain.rb
    Library/Formula/libtool.rb
    Library/Formula/libvbucket.rb
    Library/Formula/libvo-aacenc.rb
    Library/Formula/libxmi.rb
    Library/Formula/lifelines.rb
    Library/Formula/makeicns.rb
    Library/Formula/mathgl.rb
    Library/Formula/meld.rb
    Library/Formula/mesalib-glw.rb
    Library/Formula/minisat.rb
    Library/Formula/minuit2.rb
    Library/Formula/mobile-shell.rb
    Library/Formula/movgrab.rb
    Library/Formula/mp3cat.rb
    Library/Formula/mpich2.rb
    Library/Formula/mrfast.rb
    Library/Formula/musepack.rb
    Library/Formula/ndiff.rb
    Library/Formula/net6.rb
    Library/Formula/nrpe.rb
    Library/Formula/nuttcp.rb
    Library/Formula/oath-toolkit.
Updating aa07533..3f070ef
Aborting
Error: Failed while executing git pull  origin refs/heads/master:refs/remotes/origin/master
chris Frisina
  • 19,086
  • 22
  • 87
  • 167
Tyler DeWitt
  • 23,366
  • 38
  • 119
  • 196

5 Answers5

745

Don't forget to fetch the origin:

cd /usr/local/Homebrew
git fetch origin
git reset --hard origin/master

What happens is that you are trying to update brew, but brew itself is either not up to date (likely), there is a permissions change via some OS update (also likely), or brew is slightly corrupt (unlikely). Since brew itself is a git repo, you have to update or reset brew to the master branch version. brew [by default] is located in the /usr/local/Homebrew folder, so you

  1. Go to that folder [first command] which also should update permissions (if not see below)
  2. Fetch the origin [second command] which means to update your LOCAL version of the remote branch of brew
  3. Hard reset [3rd command] based on the REMOTE master branch (which also uses your current permissions).

You can also chown the first command if you are in a non sudo or admin profile

sudo chown -R `whoami` /usr/local/Homebrew
cd /usr/local/Homebrew
git reset --hard origin/master

To understand git reset, take a look at this article.

double-beep
  • 5,031
  • 17
  • 33
  • 41
chris Frisina
  • 19,086
  • 22
  • 87
  • 167
  • 3
    I had to `git fetch origin` before the reset worked. Thanks. Funny how this answer was added in the past day, lucky me! – ghoppe Aug 20 '12 at 17:37
  • 3
    `/usr/local` `git fetch origin` `fatal: 'origin' does not appear to be a git repository` `fatal: The remote end hung up unexpectedly` – Rich Bradshaw Feb 23 '13 at 11:55
  • @RichBradshaw 'origin' refers to the 'remote' repo. your issue seems to be either a connection issue (unlike if using GHub), a reference error (your url is bad), or likely that you are pointing to the wrong repo, maybe of a different type (ie SVN) – chris Frisina Feb 23 '13 at 17:18
  • In the end I just wiped the whole thing and started from scratch. PITA, but things are working again! – Rich Bradshaw Feb 24 '13 at 10:09
  • 2
    Have a look at this https://github.com/mxcl/homebrew/wiki/Common-Issues The error is described and how to correct it too. – Stephane Paquet May 12 '13 at 19:12
  • Why does this occur? Is it a bug, or result of some error, or just in the nature of brew? – oskob Jul 01 '13 at 18:28
  • @oskob it is the result of an old bug. Resetting form the new origin fixes it. – chris Frisina Jul 01 '13 at 22:27
  • You need to go to your homebrew directory to do this, I'm using boxen, so mine was at /opt/boxen/homebrew/ – jakecraige Aug 13 '13 at 16:21
  • 11
    It worked, but I had to do the last step with `sudo` – altumano Nov 08 '13 at 11:27
  • 3
    this still happens to me regularly, infact I've needed to repeat these steps 3 times in the last few months. Quite annoying, it's handy that SO has a favourite feature (that said googling the error brings this post up as first result usually) – totallyNotLizards Jan 10 '14 at 14:59
  • 2
    Piece of advise from https://github.com/Homebrew/homebrew/issues/2906 works for me: "sudo chown -R `whoami` /usr/local", "cd /usr/local", "git reset --hard origin/master" – Denis Feb 17 '14 at 13:46
  • @Denis your solution worked for me. seems like this is a permissions issue – kumikoda Mar 20 '14 at 22:29
  • I don't know how developers put up with software like brew that just break and require you to do some git fiddling to fix them. Maybe brew should have some option like "fix" or brew "reset". – iphone007 Oct 05 '14 at 02:14
  • @chrisFrisina Thanks, I had the same issue and your answer resolved it. One question that confuses me: you are going a `git reset --hard origin/master` in /usr/local which seems a common, generic directory to me, rather than something brew-specific. Won't `git reset` mess up other checkouts or git-controlled things in there? It seems so strange to me that something entirely *not* aimed at brew in particular, is used to fix an issue that is specifically related to brew. – RocketNuts Jul 05 '15 at 10:58
  • @RocketNuts It won't mess it up. `/usr/local` is separate from `/usr` in that it doesn't get overwritten by system updates (but can get changed via other software or commands, and has neat permissions/sharing with OS accounts, which can get complicated, hence why homebrew was created and still maintained. The `brew` command fails, hence why you need to update homebrew by resetting it via Git. – chris Frisina Jul 07 '15 at 16:06
  • For me it was inside of `/usr/local/Homebrew` instead of just `/usr/local` – Thomas Gotwig Oct 17 '20 at 17:29
107

I had a similar issue a couple weeks ago trying to update an old homebrew install. Doing this:

git reset --hard origin/master

in /usr/local fixed it for me.

It seems like other folks have had this issue too. Have you looked over any of the proposed workarounds here?

Joe Holloway
  • 28,320
  • 15
  • 82
  • 92
  • Worked like a champ. Thanks for the link too! – Tyler DeWitt May 28 '12 at 16:25
  • Note to others: do try running 'git fetch origin' as suggested in the other answer -- only doing that properly fixed this issue for me (there was no obvious problem without 'git fetch origin' other than brew not seeing any updates ;) ). – Gilead Sep 17 '12 at 13:20
  • I just ran across this brew error this morning. A little googling lead me to here. This answer totally solved my problem. Thanks! – memoht May 16 '13 at 15:12
  • 2
    first you may have have to ``cd usr/local`` and ``git remote add origin https://github.com/mxcl/homebrew.git`` see also http://stackoverflow.com/questions/6024671/how-do-i-update-homebrew#comment7019176_6024671 – s2t2 Jul 27 '13 at 19:51
12

I'm adding my personal experience, since it seems a little safer than what proposed in 2012:

  1. Run brew doctor.
  2. If you get the following warning:

    Warning: The /usr/local directory is not writable.
    

    run:

    sudo chown -R `whoami` /usr/local
    

    to fix the permissions problems (as suggested also by Chris Frisina). Eventually run brew doctor again to ensure yourself that the warning is gone.

  3. Now, you should have a

    Warning: You have uncommitted modifications to Homebrew
    

    that may bey solved by

    cd /usr/local/Library && git stash && git clean -d -f
    

    as suggested by Dr.Brew itself. The command stashes the uncommitted modifications so you could go back and recover them if needed. It seemed safer than git reset --hard origin/master to me.

  4. If you wish, check the official troubleshooting guide if the steps suggested here and by other SO users does not solve your problem.

furins
  • 4,979
  • 1
  • 39
  • 57
0

Similar answer but if you have files that are no longer tracked you need additional step so from /usr/local run

git fetch origin
git clean -f
git reset --hard origin/master
Haris Krajina
  • 14,824
  • 12
  • 64
  • 81
0

This approach may be simpler than some. It involves:

  • fixing a git issue so you can delegate management of changes to it again.
  • no manual moves of files or directories.
  • no manual adjustments of file or directory permissions.

Steps (with notes for those who want explanations):

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 

Overview:
From what I can tell, the actual cause of this issue is a change in the repo url. It's now brew and was brew.git. (Full up-to-date url: https://github.com/Homebrew/brew)

Note 1: This first command takes you from anywhere in your file structure to the correct directory. The directory structure is different for me than what others show above (Mac OS 10.11.16), but with this command, those differences should not matter.

Note 2: This second command adds the correct remote url to a new alias; I did so just in case this approach didn't accomplish what I wanted and I needed the previous address again. Since the new remote worked, I'll invite someone else to comment on simply changing the url aliased by origin. I'll happily update the answer to reflect what worked for you.

Note 3: This forth command has exactly the desired result: it reports a large number of updates, including the particularly nice report of "==> Migrated HOMEBREW_REPOSITORY to /usr/local/Homebrew!" (emphasis theirs).

Kay V
  • 3,738
  • 2
  • 20
  • 20