Switch user in GitHub CLI
Asked Answered
U

3

7

I am using GitHub CLI and MinGit for Windows.

When I logout of GitHub CLI with gh auth logout and log back in with different account by calling gh auth login git config does not change.

When I type git config --global --list the user.name and user.email entries still point to the old account. So when I commit, it looks like the commit was made by my old account.

I know I could type git config --global user.name "username" and change the name but how can I do this correctly with GitHub CLI?

If I cannot do this with GitHub CLI are user.name and user.email the only two entries I need to change to commit as my new account?

Unblown answered 24/3, 2021 at 9:2 Comment(0)
D
7

A new command has been added. You can use the following

gh auth switch

For further information you may check this out https://cli.github.com/manual/gh_auth_switch

Dysgenic answered 13/12, 2023 at 13:40 Comment(2)
This is awesome, didn't know about itSecant
For me gh auth switch doesn't change git's user.name and user.email. gh auth setup-git didn't help. However accounts switch correctly I can pull private repos via both accounts. Any ideas?Homestretch
C
6

Setting the environmental variable GH_CONFIG_DIR allows you to change where gh cli looks for your authorization/credentials.

This means that you can use a tool like DirEnv to set up a different GitHub authorization in each project/directory.

For example:

cd project_dir/
echo $GH_CONFIG_DIR # likely returns blank
gh auth status # will show you logged in to account 1

export GH_CONFIG_DIR=.config # set GH_CONFIG_DIR to local .config
gh auth status # will now show you not logged in to any account

gh auth login # you can now log in to account 2

Cookson answered 8/5, 2022 at 3:22 Comment(1)
How does this affect the .gitconfig where the committer's name and email are stored?Unblown
S
-1

Changing Your Committer Name & Email Globally

ex-:

git config --global user.name "John"
git config --global user.email "[email protected]"

Changing the Author Information Just for the Next Commit

git commit --author="John<[email protected]>"

learn more -: https://www.git-tower.com/learn/git/faq/change-author-name-email/

Stichomythia answered 24/3, 2021 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.