174

I have an issue with git and my terminal. Here's a gallery to show you my issue.

When I push commits from my terminal, git says I push them with another username, that's a user from my organization (my company) with no commit at all and it seems it belongs to no one : (check gallery first pic)

But this doesn't happen when I use Github for mac, in the feed I see the commits pushed by myself.

The problem also affects my personal repositories, my terminal says that I don't have the permission to push commits on those repositories (which is obviously wrong) since it tries to push it with this user : (check gallery second pic)

Guess what ? This doesn't happen with Github for mac too.

I changed my computer to a brand new one few days ago, so I reset'ed all my ssh key of github and left only a new one generated by Github for Mac so I don't think that there's some ghost user/ssh key hidden somewhere, this hdd is brand new : (check gallery third pic)

My .gitconfig file is all clear, there's only my credentials : (check gallery fourth pic)

I really don't get it, help, StackOverflow, you're my only hope.

(My apologies for my poor Gimp skills and the Star Wars reference)

EDIT : ssh-add -l only shows the good ssh key created by github for mac and I have only one github account

EDIT2 : ssh -T [email protected] recognize me as the good user.

EDIT3 : After a few tests it looks like my terminal does the commits with my username, but pushes them with the other one, Github for mac commits and pushes with the good username.This situation happen with every repo I have/make (even new ones).

EDIT4 : In a personal repository git log --pretty="%h %an %ae" shows my good username

EDIT5 : No sign of environment variables that would override my credentials in my env. Even if I try to set those variables with the good credentials problem persists.

EDIT6 : Things work back normally if I force the user in the path of /.git/config of a repository but I don't think that's the good option : http://[email protected]/USER/REPO.git

EDIT7 : We deleted the git user that pushed the commits for me and this brings another error : remote: Invalid username or password. fatal: Authentication failed for 'https://github.com/USER/REPO.git/'

FINAL EDIT : I installed git with homebrew, typed git config --global push.default simple and now it takes my credentials even without forceing the user. That's strange. Thanks everybody for your help, you're great guys !

13
  • 3
    check ~/.gitconfig and $project_root/.git/config files. One of those two is surely misconfigured for user name. Feb 6, 2014 at 22:31
  • 1
    Thanks for your answer ansh0l. ~/.gitconfig is clear and so is $project_root/.git/config. In fact I have this issue with every personal project, work projects can be pushed since this other user belongs to my organisation that owns those repositories.
    – Yinfei
    Feb 6, 2014 at 22:34
  • 1
    Do you have multiple github accounts then? One for company, the other for personal usage? Feb 6, 2014 at 22:36
  • Nope, only one for everything.
    – Yinfei
    Feb 6, 2014 at 22:36
  • 1
    An annoying solution would be to just regenerate another SSH key. If you are using your current SSH key with another service it would be pointless. Feb 6, 2014 at 22:40

32 Answers 32

199

I just had this problem at work. The builtin git that ships with mac or comes when you install xcode caches git credentials in keychain. The fix for me was to:

start keychain access (start spotlight via cmd + space, type keychain, press enter)

Under keychains on the upper left, select "login" Under category on the left, select "passwords"

find the name "github" and delete it.

6
  • 3
    Make sure you delete all github entry here & set the login configuration *git config --global user.name <name> *git config --global user.email <email> Mar 9, 2015 at 8:07
  • In my case, SourceTree was experiencing this problem. Deleting the item in the keychain fixed it!
    – Shoerob
    May 1, 2015 at 19:00
  • 1
    @Shank_Transformer your solution worked for me! Thank you!
    – Nazariy
    May 3, 2016 at 19:22
  • You may have to search for (or launch directly) seahorse on Ubuntu.
    – caw
    Jun 11, 2017 at 23:13
  • Was this a permanent fix? Or did you have to do this every time you switched users? Dec 10, 2019 at 9:37
103

GitHub identifies you by the ssh key it sees, not by any setting from git.

Therefore, you need to ensure that your work account's ssh key is not in your keyring when you try to push from your personal account and vice versa.

Use ssh-add -l to determine which keys are in your keyring, and ssh-add -d <keyfile> to remove a key from your keyring, if it dosent work remove the 'unwanted' ssh key from ~/.ssh/config.

source

NB: GitHub will still identify your commit based on the email only.

8
  • 12
    This is the only answer that explains why it was persistently using the wrong github account despite my user.email being set correctly. Wish I could upvote five times.
    – Chris
    Nov 28, 2017 at 6:29
  • 4
    in my case removing ssh key worked like a charm ssh-add -D
    – rPawel
    Jan 3, 2018 at 16:15
  • 1
    This was the only answer that worked for me. Thanks!! May 23, 2018 at 13:54
  • 1
    When I do ssh-add -l I get an answer that doesn't help. It types out 4096 SHA256:lotsOfGibberish,about40chars (RSA) I don't know how to us this information. Nov 17, 2018 at 5:42
  • 2
    "github identifies you by the ssh key it sees, not by any setting from git" - IMHO, this is inaccurate and very misleading/wrong. When you are using SSH to interact with github, the permissions are checked by the SSH. However, github determines the authorship of commits based on the email gitconfig regardless of the SSH key. Mar 22, 2019 at 21:31
44

I'm using Windows 10 and I faced the same issue today. In my case my credentials for different user were saved by Windows Credential manager. Thus deleting/unsetting git credentials with below command, git config --global --unset credential.helper

didn't help. I had to manually delete the entry in Windows by following the below way,

Start --> Control Panel ---> User Accounts ---> Manager your credentials ---> Windows Credentials

Then search for an entry like, git:https://github.com and remove it. It works fine after that.

1
  • You say it "didn't help" but if it gave you the message that it couldn't lock the config file then the problem is simply that you need to run it from an elevated Command Prompt, as was pointed out in a comment above.
    – Stefan
    Jul 30, 2020 at 20:30
30

A temporary solution is first run killall ssh-agent then add the ssh keys generated for the account you need to use ssh-add ~/.ssh/id_4shameer

It will help us to work on multiple github account when we get the error of type ERROR: Permission to user/repo-git.git denied to username.

2
  • 2
    I did that after having removed any Github entry from MacOS keychain, and it worked perfectly. Two points, after the killall, the ssh-agent has to be restarted with eval "$(ssh-agent -s)", and the ssh-addcommand has to be executed with sudo.
    – arvymetal
    Jul 25, 2017 at 15:42
  • This was the only solution that worked for me. After doing so, I also had to regenerate a new ssh key and add it to the agent, add it to my github account, and then reauthenticate with github. Then I did ssh -T [email protected] to re-authenticate with github, and then everything went back to normal. Thank you!
    – ttemple
    Jul 31, 2023 at 20:56
25

it looks like my terminal does the commits with my username, but pushes them with the other one

Author and committer name and email (which are important for GitHub) are derived from:

git config user.name
git config user.email

However, as mentioned in git config and git commit-tree, those values can be overridden by environment variables:

GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL
GIT_COMMITTER_NAME
GIT_COMMITTER_EMAIL

So double-check those variables.

Things work back normally if I force the user in the .git/config of a repository but I don't think that's the good option.

But it should be a good solution.
When using an https url, I always specify the user in it to make sure the authentication is done with the right user.

http://[email protected]/USER/REPO.git
11
  • Thanks for your reply VonC ! Unfortunately, my git configcredentials are right and there's no environnement variables set in my /username/.bashrc file...
    – Yinfei
    Feb 7, 2014 at 9:36
  • @Yinfei84 nonetheless, check your 'env' output.
    – VonC
    Feb 7, 2014 at 9:41
  • @Yinfei84 what would happen if (to test it out) you set those variables explicitly, and try a commit and a push. Would that work better then?
    – VonC
    Feb 7, 2014 at 10:43
  • 2
    Excuse me if I wast clear, I meant that it worked if I force the user on the path like : http://[email protected]/USER/REPO.git
    – Yinfei
    Feb 7, 2014 at 11:41
  • 1
    http://[email protected]/USER/REPO.git is actually the ONLY workable option. In other cases Git will send password of random user to remote servers, usually not owned by the user.
    – wvxvw
    Feb 25, 2018 at 11:37
21

Despite all the great options given by other users, the only way to fix this was to reinstall git completely and type git config --global push.default simple to rewrite good credentials.

8
  • @VonC, despite your answer was great, it didn't work at all. This is the only solution that worked for me. I wonder if it's git issue or OSX... Mar 25, 2016 at 7:05
  • 52
    git config --system --unset credential.helper worked for me, I'm now asked for my GitHub credentials on push again and can supply the correct user ID and password.
    – CodeManX
    Sep 2, 2016 at 12:44
  • 2
    @CoDEmanX your's was the only answer which worked for me. For anyone else who comes across this, it was because we use 2FA at work with github and I needed to generate a token from the github gui first and use that as my password from the command line after I had reset my local credentials ! check out https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ for details May 9, 2018 at 9:14
  • Its just the .gitconfig file . Either remove that or reset it... I faced this problem when I submitted a trial project for an interview. Damn it hurts though. Its with the config file Jul 22, 2018 at 18:23
  • git config --system --unset credential.helper does not work on Windows 10 git bash: error: could not lock config file C:/Program Files/Git/mingw64/etc/gitconfig: Permission denied
    – alex
    Sep 30, 2019 at 16:17
17

What a pain in the butt!

problem:

  1. create a repo on git hub
  2. clone to local machine
  3. can not push 403.

turns for unknow reason git push was using the wrong user. I have a couple of different git hub user ids. I work for 2 different companies and also have a student id

I am working on a mac. here is what I finally did

1) remove the credential.helper

  • some how the credential helper got set to osxkeychain
  • when I started keychain, click on login, passwords and searched for github I found 3 entries. I have no idea how keychain could possible know which one to use

a. you need to figure out where the credential.helper is configured

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

b. once find the correct fig file remove it as follows

git config --global --unset credential.helper

Now in my local repo I hacked the .git/config file

I changed

    url = https://github.com/aedavids/lab3RotationProject.git

to

    url = https://[email protected]/aedavids/lab3RotationProject.git
2
  • For me it was good enough to just do step 2 (editing the local .git/config) -- no need to clear the cache.
    – Jasha
    May 19, 2021 at 4:41
  • Ditto - simply editing the config file to provide the correct user name for github fixed the problem for me. Thanks a lot!
    – Nik Bhatt
    Apr 6, 2022 at 21:53
12

If you are using MAC, then go to Keychain Access and remove the entry of the user for which you don't want git access.

6

This is how you debug and fix it for ssh url

First, check which user is via ssh:

ssh -T [email protected]

Hi <your_current_github_username>! You've successfully authenticated, but GitHub does not provide shell access.

Login GitHub with the above your_current_github_username Check all configured ssh keys: https://github.com/settings/keys

Method 1:

Remove the ssh key from your_current_github_username

Method 2:

  • determine which key was associated with your_current_github_username

    cat ~/.ssh/id_*.pub
    
  • Assumming id_rsa is the SSH key of your target github username, in your case it's might be a different file name.

    eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_rsa
    

Method 3:

If no public key was found on your machine (or you've lost it) Then create a new ssh key by ssh-keygen and place it here: ~/.ssh/id_new_keyyyy

eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_new_keyyyy
1
  • Thanks, an important thing this solution brings up (particularly if using github) is making sure you actually have ssh keys configured in github.com/settings/keys . May apply for other remotes that require an ssh key vs. say https style username/password login.
    – Vivek Gani
    Jul 11, 2023 at 2:40
5

The solution for me was to add an entry in my ~/.ssh/config file for github. I had to do this because:

  1. I had multiple github accounts with the same key (don't do this!)
  2. when you 'git push' using ssh, your computer grabs id_rsa by default and uses that as its ssh identity.
  3. github can't (unsurprisingly) deconflict which account you mean, since it's basing the account off the key it is presented, which if tied to more than one account, leads to pain like this. The killer is, for a long time, I was getting away with this and things just worked out.

The entry I added is:

Host github.com
    Hostname github.com
    Port 22
    User waterproofpatch
    IdentityFile ~/.ssh/id_rsa_waterproofpatch

I had created a new key, unique to my account, as id_rsa_waterproofpatch. This entry in my ssh config specifies that for connections to github.com, I wish to present this key.

Another solution would probably have been for me to log into the other account, delete the duplicate ssh key.

3

I had a similar issue and it turned out that the problem was the fact that the public key file contained my email address on the last line. That seemed to override the User setting in my config. As soon as I removed my email from the .pub file (and re-uploaded to my repo) Git connected using the correct user.

1
  • This worked for me. Not sure why the identifier on the public key file was originally in the format 'name@domain' but it was. May 19, 2020 at 15:56
3

This has been irking me for around 2 years and I finally took the time today to figure it out. Anything I force-pushed would show up as pushed by a different user on GitHub web or as triggered by a different user on CircleCI web. The culprit seemed to be that my local repos, lazily created via Code > Open with GitHub Desktop from my fork on GH web, were all using HTTPS.

If you're like me, your remote should look like this:

> git remote -v
origin  https://github.com/USERNAME/REPO.git (fetch)
origin  https://github.com/USERNAME/REPO.git (push)
upstream    https://github.com/ORG/REPO.git (fetch)
upstream    https://github.com/ORG/REPO.git (push)

Then I:

  • Ran git config --system --unset credential.helper to clear out my HTTPS credentials; this may not have been necessary so you can try skipping it unless you're in a scenario where you have some bad/invalidated stored credentials.
    • If you're on Mac OS you can also open the Keychain utility application and view/modify/delete individual GitHub.com Internet Password credentials stored in your login keychain. The Access Control tab for these credentials will show a little terminal icon with the name git-credential-osxkeychain under 'Always allow access...'
    • Alternatively, you can play with them in the terminal via git credential-osxkeychain
  • Ensured I had SSH set up by running ssh -T [email protected], which printed a response confirming I was authenticated.
  • Reconfigured my remotes to use the SSH urls from my fork:
> git remote remove origin
> git remote add origin [email protected]:USERNAME/REPO.git
> git remote remove upstream
> git remote add upstream [email protected]:ORG/REPO.git

# Shorter alternative:
> git remote set-url origin [email protected]:USERNAME/REPO.git
> git remote set-url upstream [email protected]:ORG/REPO.git

Your next pull may tell you to re-set up your upstream tracking branch (the error message prints the syntax for this if you aren't familiar with it).

I suspect that force push events are attributed to a user via credential, and that there is some issue with identifying the user when pushing from an HTTPS clone; to prevent a totally broken experience when a push can't be attributed to a user, there is probably some logic on GH's side that arbitrarily chooses an org user to attribute the event to. Things like ensuring an author was set (even though my user info was displaying as author perfectly fine in the commit log) did not work for me; it makes sense that they wouldn't resolve this issue, because a push may contain many new commits from a variety of different authors. The moral of this story is always use SSH.


Side note: Be wary of any solutions that tell you to update your git config to automatically replace the HTTPS repository URL with an SSH repository URL. There are certain things that approach will very confusingly break, like homebrew taps in private repos (which as of 2021 still only supports HTTPS).

1
  • 1
    git config --system --unset credential.helper this is the magic command that resolved my issue, thanks. It cleared the stored credentials and I was able to log in as a different user.
    – Papooch
    Jun 24, 2021 at 7:26
2

First Go to this path in Windows
Start --> Control Panel --> User Accounts --> Manager your credentials --> Windows Credentials

And Remove the credential from git:https://github.com

Then add your GitHub account username and password from which you want to push the code at the same place.

To check whether git will push from the username that you added now enter the following command in git bash.

git config user.name

This will show you the username from which the git will push. If the username is correct then your problem is solved.

For me this didn't solve the problem it was still showing my previous account username that I removed.

Now enter the username and email Id of the account from which you want to push in the following command in git bash.

git config --global user.name your_username
git config --global user.email your_emaiid

This will now change the user.
Before pushing any code you can verify whether the user is changed or not by again using the following command.

git config user.name
2

The comment from @bolun-zhang helped me finally solve this:

"github identifies you by the ssh key it sees, not by any setting from git"

IMHO, this is inaccurate and very misleading/wrong. When you are using SSH to interact with github, the permissions are checked by the SSH. However, github determines the authorship of commits based on the email gitconfig regardless of the SSH key.

For me, this turned out to be completely accurate. Reguardless of what I set in ~/.ssh/config even using: export GIT_SSH_COMMAND="ssh -i ~/.ssh/some-key", an alternate username was still being selected.

The problem and solution was that git config user.email was matched to the alternate email of another github user.

After removing that email from the other github user, the commit worked fine.

Ultimately, as @venkatesh-murugesh pointed out, you need to set:

git config user.email

To match the email address of the github user account that should own the commit.

Of course, you'll want to use ~/.ssh/config or GIT_SSH_COMMAND to make sure that SSH is using the correct key. Test it with:

ssh -T [email protected]
2

Steps to add new ssh key to a new git repo on win10:

  1. Generate and save ssh key pair in your .ssh directory. I assume you made them in correct form, because putty gen can generate funny key types by default.
  2. You should have config file in ssh directory (add it as .ssh/config if it doesn't exist yet):

Host github.com // this is for your main default key; remove comments
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa // your current main key; remove comments

Host whatever-host-nickname-you-want
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_second-key // path to new key; remove comments

  1. In your repository where you need a second key do

git config user.name username-from-second-account
git config user.email email-from-second-account

  1. Now change origin in your reposiory usingp reviously set host nickname and username & repo name from new account:

git remote remove origin
git remote add origin git@whatever-host-nickname-you-want:username-from-second-account/repository-name.git

You should be good to go.

2

Clearing keychain didn't help (I'm on a Mac )

This is what worked for me:

  • Press +Space (aka Cmd+Space), look for "Keychain Access" and open it.
  • Search for all github credentials and delete them.
  • Open Terminal and enter:
    git config --global --edit
    
  • Go into edit/ insert mode by pressing letter i on your keyboard.
  • Locate [user] section, and edit your email address (type the right user email address).
  • Press Esc and type :wq to save changes and exit.
  • Check that you have right information by verifying it one last time:
    git config --get-regexp '^user\.(name|email)$'
    
1
  • just to add to this - I went into my local instead of global and changed [remote "origin"] url from [email protected] to the name of my SSH config host
    – ness-EE
    Feb 20 at 14:09
1

What worked for me was to use the Github repo's https URL instead of the ssh URL. I went to the Github project page and copied the https URL into my clipboard, and then pasted it into the second command below:

git remote rm origin
git remote add origin https://[...]
1
  • 1
    for me it was the opposite i had a https:// url that gave me 403 with wrong user id ?? switched to ssh and all good now.
    – Pieter
    Nov 12, 2023 at 9:56
1

In my particular case, the issue was that I was using a .netrc to access github.com and it was configured with a token from a different user:

machine github.com login <another-user-token>
1

Just spent 6 hours figuring this out when trying to push to a new GitHub pages repo on a new account.

Even after setting the config user.name and user.email it would default to my main account.

This is because the ssh key will default to id_rsa (my main account).

To use the new account I had to run:

ssh-add ~/.ssh/my_new_key

which will then make git use the new key when pushing.

1

I had a similar issue while running git via Remote SSH inside of Visual Studio Code. Turns out that VSCode by default sets itself up as authentication handler and sets $GIT_ASKPASS to a script ($HOME/.vscode-server/bin/*/extensions/git/dist/askpass.sh), which it uses to set/override git credentials.

This can be overridden by setting git.terminalAuthentication to false (aka. Git: Terminal Authentication) and effectively removes the $GIT_ASKPASS environment variable from the terminal.

1

It might be because of bad remote URL. You can check it with: git remote -v.

To replace it:

git remote remove origin
git remote add origin git@<user>:<user>/<repo>.git
2
  • this is wrong. How git will know which remote host you are using. Like github, bitbucket, gitlab etc. Dec 12, 2021 at 19:46
  • Git will know this thank to ssh config, see my answer Jun 22, 2022 at 7:42
1

I faced the same issue while using several github accounts and despite that push requests were going through the correct username, on github user were set the main account used in --global configuration.

So, the simple fix is just to use local configuration: git config --local user.email second_account_account

That solved the problem for me.

2
  • Thank you! this local config thing is overlooked by most answers.
    – DinoSaadeh
    Feb 8, 2022 at 7:51
  • It doesn't work for me, it still prompts that the other user (defined in global) is denied.
    – Nour
    May 23, 2022 at 20:19
0

clearing keychain didn't help... I had to ssh-add -D and re-add the key with ssh-add <keyfile>

0

I solved this problem removing (or renaming to *.bak) the id_rsa and id_rsa.pub file on MacOS High Sierra. Idea from here.

I have custom host redirects in ~/.ssh/config that should be applied but used wrong user before I renamed the two files...

0

That's what worked for me:

  1. Changing the credentials inside .git-credentials
  2. Changing the global user.name and user.email inside .gitconfig
0

What worked for me removing the repo and adding it again:

git remote rm origin
git remote add origin [email protected]:fguillen/MyApp.git
0

I have the same problem in windows10 even after uninstall my git, as @user542833 says it is because windows cache and you should remove Github credentials in your windows Credential Manager and when you again attempting to push, windows ask your credential and set it again

1
  • this worked sometimes for me, but also sometimes it didn't.
    – bvdb
    Nov 20, 2019 at 12:50
0

No matter what I tried I couldn't get the name to change on github because the commit itself needed a different author:

git commit --amend --author="Author Name [email protected]"

Now the commit shows the correct account.

0

Had exactly the same issue on my side.

The solution was simple:

Open the local or global git config file, and provide the [author] email explicitly.

enter image description here

0

Using this command you can easily pushed to git under a different user name.

git add .
git commit -m "initial commit"
git config --local credential.helper ""
git push https://github.com/youraccount/repo.git --all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.