How to rename formula installed via Homebrew?

I installed gcc with homebrew:

$ brew install gcc

I can see the result:

$ brew list
ant         gcc     go          libyaml     postgresql  tmux
autoconf    gdb     isl         mpfr        python      valgrind
automake    gdbm    libevent    openssl     readline    wget
binutils    git     libmpc      ossp-uuid   ruby        xz
cloog       gmp     libtool     pkg-config  sqlite

I also see gcc-4.9 in /usr/local/bin

$ ls /usr/local/bin
...
gcc-4.9
...

The issue is that typing gcc still refers to the system gcc

$ which gcc
/usr/bin/gcc

I wanted to rename gcc-4.9 to gcc, but I don't want to break anything in homebrew. Specifically, I'd like to rename it in such a way that homebrew is aware of the change and running brew update will know to link the newly updated version to the name gcc in /usr/local/bin.

Can this be done?

DESIRED BEHAVIOR:

$ which gcc
/usr/local/bin/gcc
$ gcc -v
... version XXX
$ brew update
... new gcc version YYY installed in /usr/local/bin/gcc
$ which gcc
/usr/local/bin/gcc
$ gcc -v
... version YYY

Solution 1:

I don't think what you want can be done directly without hacking Homebrew formulas, er, I mean formulae.

What I do is put

export CC=gcc-4.9

in my shell profile. That works in almost all scenarios I'm interested in.

An alternative is that you create your own "bin" directory, say ~/bin/, put that first in the path, and symlink ~/bin/gcc to /usr/local/bin/gcc-4.9.

Both of these methods will survive package upgrades.