Local git config not overriding global user for project

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
[email protected]
...

git config --local --list:

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

git config user.name:

localuser

Solution 1:

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:

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:

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:

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.