Github-cli同时管理多个帐号

由于工作和学习的需要,可能需要同时在一台电脑上维护多个 github 帐号,这时使用 Github-cli 同时使用多个帐号的 Token 认证后,然后就可以随时切换了。具体方法为:

登录认证帐号

1
gh auth login -p ssh --skip-ssh-key --with-token < your_token_here >

注意: 这条命令选择了 ssh的认证方式,所以需要提前配置好 ssh. 同时按 使用Github-Cli管理仓库 取得 Token.

切换帐号

1
gh auth switch -u < your_name_here >

gh auth 选项详解

gh help auth login
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Authenticate with a GitHub host.

The default hostname is `github.com`. This can be overridden using the `--hostname`
flag.

The default authentication mode is a web-based browser flow. After completion, an
authentication token will be stored securely in the system credential store.
If a credential store is not found or there is an issue using it gh will fallback
to writing the token to a plain text file. See `gh auth status` for its
stored location.

Alternatively, use `--with-token` to pass in a personal access token (classic) on standard input.
The minimum required scopes for the token are: `repo`, `read:org`, and `gist`.

Fine-grained personal access tokens are not supported.

Alternatively, gh will use the authentication token found in environment variables.
This method is most suitable for "headless" use of gh such as in automation. See
`gh help environment` for more info.

To use gh in GitHub Actions, add `GH_TOKEN: ${{ github.token }}` to `env`.

The git protocol to use for git operations on this host can be set with `--git-protocol`,
or during the interactive prompting. Although login is for a single account on a host, setting
the git protocol will take effect for all users on the host.

Specifying `ssh` for the git protocol will detect existing SSH keys to upload,
prompting to create and upload a new key if one is not found. This can be skipped with
`--skip-ssh-key` flag.

For more information on OAuth scopes, <https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps/>.


USAGE
gh auth login [flags]

FLAGS
-p, --git-protocol string The protocol to use for git operations on this host: {ssh|https}
-h, --hostname string The hostname of the GitHub instance to authenticate with
--insecure-storage Save authentication credentials in plain text instead of credential store
-s, --scopes strings Additional authentication scopes to request
--skip-ssh-key Skip generate/upload SSH key prompt
-w, --web Open a browser to authenticate
--with-token Read token from standard input

INHERITED FLAGS
--help Show help for command

EXAMPLES
# Start interactive setup
$ gh auth login

# Authenticate against github.com by reading the token from a file
$ gh auth login --with-token < mytoken.txt

# Authenticate with specific host
$ gh auth login --hostname enterprise.internal

LEARN MORE
Use `gh <command> <subcommand> --help` for more information about a command.
Read the manual at https://cli.github.com/manual
Learn about exit codes using `gh help exit-codes`

参考文章