Git Error: Could could not read Username for 'https://github.com': terminal prompts disabled

Today I ran into this error while trying to update Homebrew.

brew update
fatal: could not read Username for 'https://github.com': terminal prompts disabled
Error: Fetching [redacted] failed!

I thought it might be an issue with Homebrew itself, but running brew doctor seemed to indicate that everything was working correctly. I know Homebrew uses a git repo (which is pretty obvious from the error message) so I thought that the git repo or git itself might be the problem. You can navigate to the repo by doing cd $(brew --repo).

After plenty of trail and error I finally found someone who had the same problem. It appears that this is a problem if you’re trying to authenticate with https but you have 2FA enabled. This can be solved by forcing git to use ssh for all interactions.

git config --global --add url."git@github.com:".insteadOf "https://github.com/"

This will use your keys in ~/.ssh - if you’re struggling with that part you can follow along in the github tutorial.

What would I have done without the internet?