GitHubのloginについてまとめています。
GitHubのlogin方法
GitHubへのlogin方法は、いくつかあります。まずは、sign inのページを開いて、ユーザ名とパスワードでログインする方法。
しかし、都度ログインするためにパスワード入力するのが煩わしいということもあります。GitHubへ都度Sing inせずに済ませる方法もあります。それは、ghと呼ばれるGitHubのcliコマンドを使う方法です。
ghを使うことで、コマンドラインからissueとpull request、リポジトリの操作やリリース、gistの操作ができるんですね。GitHubのウェブページの全機能が使えるというわけではありませんが、これだけできれば十分という方も多いのではないでしょうか?
GitHub cli(gh)のubuntuへのインストール
以下は、ubuntu 18.04にghをインストールする手順です。ghはwindows、mac、linuxに対応しています。ubuntuでは、以下のようにaptコマンドにてインストールが可能です。
$ curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo gpg --dearmor -o /usr/share/keyrings/githubcli-archive-keyring.gpg $ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null $ sudo apt update ヒット:1 http://jp.archive.ubuntu.com/ubuntu bionic InRelease 取得:2 http://jp.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB] 取得:3 http://jp.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB] : : $ sudo apt install gh パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 : :
以下は、ghのヘルプです。(バージョンは1.11.0です。)
gh Work seamlessly with GitHub from the command line. USAGE gh <command> <subcommand> [flags] CORE COMMANDS gist: Manage gists issue: Manage issues pr: Manage pull requests release: Manage GitHub releases repo: Create, clone, fork, and view repositories ACTIONS COMMANDS actions: Learn about working with GitHub actions run: View details about workflow runs workflow: View details about GitHub Actions workflows ADDITIONAL COMMANDS alias: Create command shortcuts api: Make an authenticated GitHub API request auth: Login, logout, and refresh your authentication completion: Generate shell completion scripts config: Manage configuration for gh help: Help about any command secret: Manage GitHub secrets ssh-key: Manage SSH keys FLAGS --help Show help for command --version Show gh version EXAMPLES $ gh issue create $ gh repo clone cli/cli $ gh pr checkout 321 ENVIRONMENT VARIABLES See 'gh help environment' for the list of supported environment variables. LEARN MORE Use 'gh <command> <subcommand> --help' for more information about a command. Read the manual at https://cli.github.com/manual FEEDBACK Open an issue using 'gh issue create -R github.com/cli/cli'
ghを使うためには、Githubへのログイン認証を行う必要があります。gh auth loginを実行します。接続先情報などを聞かれるので、以下のように指定します。なお、選択肢を選ぶには矢印キーの上下で操作し、Enterキーで確定です。
$ gh auth login ? What account do you want to log into? GitHub.com ? What is your preferred protocol for Git operations? HTTPS ? Authenticate Git with your GitHub credentials? Yes ? How would you like to authenticate GitHub CLI? Paste an authentication token
「Paste an authentication token」とは、認証トークンを貼り付けるという意味で、認証トークンはGitHubの設定画面から入手できます。
なお、How would you like to authenticate GitHub CLI?(GitHub CLIをどのように認証する?)に対して、「Login with a web browser」を選ぶと、ubuntuのターミナルの場合はlynxというテキストブラウザが起動し、その後の操作がわかりにくいのでおすすめしません。
認証トークンを取得するために、ウェブブラウザでGitHubにログインします。ログイン後、画面右上のアイコンをクリックし、Settingsを選択します。
Developper Settingsをクリックします。
Personal access tokenをクリックして、Generate new tokenをクリックします。
New personal access tokenという画面になります。Noteに何に対するトークンなのかわかりやすいよう書いておきましょう。今回は「for Gh」と記入しました。
さらに、Ghに与える権限にチェックをつけていきます。Ghを動作させるには最低限、repo、workflow、read:orgの3つにチェックをつけておく必要があります。
必要な権限にチェックをつけたら、画面を下にスクロールさせ、Generate tokenをクリックします。
Personal access tokens(認証トークン)が表示されます。トークンコード右のクリップボードアイコンをクリックして、コードをコピーします。
ubuntuのターミナルに戻って、Paste your authenticasion tokenにコードをペーストします。最後に「logged in asユーザ名」と表示されれば、ghでのログインは成功です。
Tip: you can generate a Personal Access Token here https://github.com/settings/tokens The minimum required scopes are 'repo', 'read:org', 'workflow'. ? Paste your authentication token: **************************************** - gh config set -h github.com git_protocol https ? Configured git protocol ? Logged in as username
GithubのLoginまとめ

- Githubへのログインは公式ページからのログインのほか、cliコマンドでのログインが可能
- cliコマンドは認証トークンを設定しておけば、都度パスワードを入力せずに使える
- Githubのcliコマンド・ghでは、issueやpull request、リポジトリ操作やリリースなどの作業が可能