Local git config not overriding global user for project

GitGithubGit Config

Git Problem Overview


I have a global git user configured, but want to use a different user for a single git project.

Within that project, I've used git config --local user.name "localuser" and git config --local user.email "[email protected]" to set the local project's user and email.

However, when I try to push to my remote on github, I get this error:

remote: Permission to localuser/repo.git denied to globaluser.
fatal: unable to access 'https://github.com/localuser/repo.git/': The requested URL returned error: 403

Here's some output that might help with diagnosis:

git remote -v:

github	https://github.com/localuser/repo.git (fetch)
github	https://github.com/localuser/repo.git (push)

git config --list:

user.name=globaluser
user.email[email protected]
...

git config --local --list:

user.name=localuser
user.email[email protected]
...

git config user.name:

localuser

Git Solutions


Solution 1 - Git

I had committed my changes and received a permission denied with my global user. Subsequently setting the local user did nothing, though git config user.name reported the correct local user.

What worked was (courtesy of this google groups thread):

git commit --amend --reset-author

I presume the committed changes had the original author attached.

Solution 2 - Git

If you are working on OSX with GitHub it might be a certificate problem. Your GitHub certificate, which remembers your user.name and user.email overrides the local config settings. One way to solve it, is to go to your keychain and remove the GitHub certificate.

Solution 3 - Git

Tried numerous ways, spent many hours but nothing worked. I had to clear all the user finally:

git config --local --unset credential.helper
git config --global --unset credential.helper
git config --system --unset credential.helper

I'm now asked for my GitHub credentials on push again and can supply the correct user ID and password :)

If using windows then it can also be deleted by going to Control Panel\User Accounts\Credential Manager

Solution 4 - Git

In my case, reset commit author does not work:

The problem is the combination of:

  • My default ssh key id_rsa.pub was set on github by my global user
  • My local user has not set the ssh-key on github

So your git will use the default id_rsa that linked to global user

The solution is:

  • use ssh-keygen to generate a new ssh key, let's say id_2 & id_2.pub, add this ssh key to github
  • git config --add --local core.sshCommand 'ssh -i <<<PATH_TO_SSH_KEY>>>' will set the ssh option with new ssh key for local repo only

Done.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionamacrobertView Question on Stackoverflow
Solution 1 - GitthekevinscottView Answer on Stackoverflow
Solution 2 - GitCasperTNView Answer on Stackoverflow
Solution 3 - GitGorvGoylView Answer on Stackoverflow
Solution 4 - GitNeo.Mxn0View Answer on Stackoverflow