gh-ma
is a Bash wrapper script for the GitHub CLI executable which makes it easier to simultaneously use multiple accounts on the same host via the use of personal access tokens. When CLI issue #326 is finally implemented the gh-ma
script will be retired.
Pairs of token ids and tokens are stored in a configuration file, and one of the tokens is chosen at runtime. A token is chosen as follows (in order of precedence):
- If any of the GitHub CLI environmental variables related to tokens are set then those are used.
- If
--token-id <ID>
is present at the start of the command line arguments then token id<ID>
is used and looked up intokens.conf
. - If
gh-ma
is invoked in a git repository with a remote origin then the id intokens.conf
with the most specific match to the remote origin URL is chosen. (If no match is found an error occurs) - If the environmental variable
GH_DEFAULT_TOKEN_ID
is set then that is used. - If the file
default-token-id.sh
is present in thegh-ma
config directory then it is sourced. If it sets the global variabletoken_id
then that is used as the token id. As an example, you could use different token ids depending upon the current working directory. - If the file
default-token-id
is present in thegh-ma
config directory then its content is used as the id.
- Rename
gh-ma
togh
and place it in a directory in yourPATH
which comes inPATH
before the directory containing the GitHub CLI executable. The wrapper script should be able to automatically detect and use the CLI executable. If it can't, then set the environmental variableGH_EXE
to the path of the CLI executable. - Create the config directory
$HOME/.conf/gh/multi-account
, or$GH_CONFIG_DIR/multi-account
if using theGH_CONFIG_DIR
environmental variable. Alternatively you can make any arbitrary directory the config directory by using the environmental variableGH_MA_CONFIG_DIR
. - In the config directory create the file
tokens.conf
to specify which accounts use which token. If you wish, you can even use different tokens for different repositories under the same account. - Optionally create the files
default-token-id
and/ordefault-token-id.sh
in the config directory.
NOTE: It is possible to use gh-ma
via aliases instead of renaming it to gh
and installing it in PATH
, but this is not recommended. Specifically, the GitHub CLI executable can rewrite the git config file so that gh
is used as a custom credential helper so that plain git
commands will use GitHub CLI authentication. If the wrapper script is only invoked via shell aliases then plain git
commands will authenticate differently than wrapped invocations of gh
.
Lines in the tokens.conf
file take the format of token_id: token
. Multiple spaces and tabs are allowed between the colon and the personal access token string. The colon can be omitted if there is at least one whitespace character between the id and the token. The token id must start at the beginning of the line, with no whitespace in front of it.
A token id has three different formats. From most specific to least specific, the formats are:
host/account/repo
host/account
host
The most specific id which matches the current repository's remote origin URL will be used, unless that choice is overridden by either any of the GitHub CLI environmental variables related to tokens or by the --token-id
command line option.
Example token ids:
github.com/google/guava
github.com/google
github.com
If the token value is either none
or blank then the wrapper script will pass no token to the GitHub CLI executable, allowing the normal authentication flow to happen.
The order of the lines is unimportant, except when the exact same token id is used multiple times, in which case the last line using that token id is the one which will count.
Empty lines and lines starting with a "#" are ignored.
Any command line option meant for gh-ma
must come at the very start of the command line arguments.
--token-id <ID>
: Specify the id of the personal access token to use. This overrides everything except for GitHub CLI environmental variables related to tokens.--version-ma
: Prints the version ofgh-ma
.--ma-info
: Prints various information about the configuration/setup ofgh-ma
. Useful for troubleshooting.--is-ma
: Exits with0
if passed to the wrapper script, exits with1
(and prints an error message) if passed directly to the CLI executable.
gh-ma
should run on any POSIX compliant system on which is installed the executables for bash
, readlink
, ln
and which
. However, it has only been tested on Fedora 33.
gh-ma
should work with GitHub Enterprise, but this has not been tested.