Just in case it helps anybody.
I have faced the same problem while executing a git push
.
This situation has been this:
- I use the same laptop for my usual day-work and my personal work. I have separate github accounts (usernames, emails etc.) for each of these two.
- I write some code. I want to check in to a repository in my personal github.com account.
- I do necessary housekeeping (commit, status, log etc.), to ensure that I am ready to push to my personal github account.
- I do a
git push
, and I am reprimanded:
remote: Permission to {my personal github username}/-------.git denied to
{my workplace github username}.
fatal: unable to access
'https://github.com/{my personal github username}/-------.git/'': The
requested URL returned error: 403
From the same laptop, I submit code my workplace github too, and for good reasons, that username is the default on my laptop. So, I understand that I have to override that.
After some exploration - including visiting this page on Stackoverflow - and trying out a few things, I have taken the following steps:
First, use the gh
, the github CLI tool (version 1.9.2 (2021-04-20), on Ubuntu 22.10), to check which user's credentials is being taken up by git, at the moment:
nirmalya@bluewhale:attempt_2_tac$ gh auth status
github.com
✓ Logged in to github.com as {my workplace github username} (/home/nirmalya/.config/gh/hosts.yml)
✓ Git operations for github.com configured to use https protocol.
✓ Token: *******************
The output underscores the reason behind that dour response to my git push
earlier.
Second, ensure that this session is brought down.
nirmalya@bluewhale:attempt_2_tac$ gh auth logout
? Are you sure you want to log out of github.com account '{my workplace github username}'? Yes /* <- default response */
✓ Logged out of github.com account '{my workplace github username}'
I am in the clear. I must be able to push now! Nah!
nirmalya@bluewhale:attempt_2_tac$ git push
Username for 'https://github.com': {my personal github username}
Password for 'https://{my personal github username}@github.com': /* I type in the password */
gh auth git-credential: "erase" operation not supported
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/{my personal github username}/whatever.git/
Third, I set myself up for a fresh login:
nirmalya@bluewhale:attempt_2_tac$ gh auth login
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations? HTTPS
? How would you like to authenticate GitHub CLI? Login with a web browser
! First copy your one-time code: D823-F390
- Press Enter to open github.com in your browser...
Opening in existing browser session.
/* at this point I move to browser and log in using my usual 2FA authentication scheme */
✓ Authentication complete. Press Enter to continue...
- gh config set -h github.com git_protocol https
✓ Configured git protocol
✓ Logged in as {my prsonal github username}
Now, I am all set to push to github, it seems:
nirmalya@bluewhale:attempt_2_tac$ git push
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 1.46 KiB | 1.46 MiB/s, done.
Total 9 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/{my personal github username}/whatever.git
aa6dc4f..b04f5e7 main -> main
Phew! :-)
Hopefully, this will save someone's time.
gh
command. I updated your tags, but it's probably best to just report this directly to thegh
command maintainers. – Ng