How can I change Git credentials to push to a different user's repository?

My roommate and I use the same laptop for our coding projects. I’ve been pushing to my own GitHub repositories without any issues since I have my credentials set up. Now, my roommate needs to push some updates to her GitHub account, but every time she attempts to do so, Git defaults to my username.

When she tries the push command, we receive an error:

$ git push origin main
ERROR: Permission denied to my_account/project.git for her_account.
fatal: Authentication failed
NOTE: Password authentication is no longer supported as of August 2021.

What steps should we take to change the Git credentials so she can push to her repository? We need to use her GitHub username and token temporarily.

just log out of the git credential manager n let her sign in with her own account. run git credential-manager-core erase, then when she pushes, it’ll ask for her login info. she should use her GitHub username n personal access token, not the old password.

Easiest fix is to temporarily embed her credentials in the remote URL. She’ll need to create a personal access token in GitHub settings first. Then run git remote set-url origin https://her_username:[email protected]/her_username/repo_name.git. Once she pushes successfully, switch it back with git remote set-url origin https://github.com/her_username/repo_name.git to keep the token secure. This skips any cached credentials and won’t mess with your global Git setup. I’ve done this tons of times when switching between accounts on shared projects.

I’ve dealt with this exact problem switching between GitHub accounts on one machine. Here’s what works: navigate to her repo in terminal and run git config --local user.name "her_username" and git config --local user.email "[email protected]". She’ll need to create a personal access token on GitHub - regular passwords don’t work anymore. When she pushes, it’ll ask for credentials. Use her username and the token. If it doesn’t prompt her, clear the cached credentials with git config --local --unset credential.helper. This keeps her Git settings separate from yours and won’t mess with your setup.