Installing older versions of a Homebrew formula (2018 edition)

For debugging purposes, I recently needed to install an older version of Git on my Mac OS machine. I obviously didn't want to permanently replace the version of Git I was already using, I just wanted them to exist side-by-side and temporarily use the older version. Since Homebrew has the built-in ability to maintain multiple versions of a package simultaneously, it seemed like a good solution.

The problem is that the old way of grabbing historical formula versions (the "brew versions" command) was recently deprecated, and all of the advice I could find seemed to be equally outdated and conflicting.

Here's the method that worked for me:

  1. Find the version of the formula you want in the Homebrew repo. For instance, for Git, I looked at the history for the Git formula here: https://github.com/Homebrew/legacy-homebrew/commits/master/Library/Formula/git.rb
  2. Click on the commit matching the version you want, and find the formula file for that version, i.e. https://github.com/Homebrew/legacy-homebrew/blob/6d0f4ea4c5a7d31c6393fd5f131418cf1c1eae1c/Library/Formula/git.rb
  3. Copy the "raw" link to that file, i.e. https://raw.githubusercontent.com/Homebrew/legacy-homebrew/6d0f4ea4c5a7d31c6393fd5f131418cf1c1eae1c/Library/Formula/git.rb
  4. Now unlink your existing version of the formula locally: brew unlink git
  5. Finally, install the new version from the raw formula link above: brew install https://raw.githubusercontent.com/Homebrew/legacy-homebrew/6d0f4ea4c5a7d31c6393fd5f131418cf1c1eae1c/Library/Formula/git.rb

After you are done testing, obviously be sure to switch back to the original package version, i.e. brew switch git 2.15.1_1. If you forget what version you had installed before, not to worry! It's still installed, you just have to find the version using brew list --versions. For good measure, manually verify that the new version is correct: git --version.