headlessgithub-cligit-authentication

How to do `gh auth login` when run in headless mode?


I'd like to do gh auth login on a headless server and still use the browser based flow (instead of the token). How do I do this?

By default nothing happens after pressing "Enter" here:

$ gh auth login            
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations on this host? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Login with a web browser

! First copy your one-time code: A123-A123
Press Enter to open github.com in your browser... 


Solution

  • The solution is simple:

    1. Run BROWSER=false gh auth login
    2. Open https://github.com/login/device in any browser, e.g. on the local computer
    3. Paste the one-time code
    4. Done

    I got this trick from https://github.com/cli/cli/issues/5500#issuecomment-1106545327