'git pushes with wrong user from terminal

I have an issue with git and my terminal.

Here's a gallery to show you my issue : http://imgur.com/a/6RrEY

When I push commits from my terminal, git says I push them with another username, that's a user from my organisation (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 !



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

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.

Solution 4:[4]

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.

Solution 5:[5]

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

Solution 6:[6]

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.

Solution 7:[7]

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

Solution 8:[8]

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.

Solution 9:[9]

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.

Solution 10:[10]

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.

Solution 11:[11]

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).

Solution 12:[12]

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]

Solution 13:[13]

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://[...]

Solution 14:[14]

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>

Solution 15:[15]

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.

Solution 16:[16]

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

Solution 17:[17]

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

Solution 18:[18]

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.

Solution 19:[19]

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

Solution 20:[20]

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

Solution 21:[21]

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...

Solution 22:[22]

That's what worked for me:

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

Solution 23:[23]

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

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

Solution 24:[24]

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

Solution 25:[25]

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.

Solution 26:[26]

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.

Solution 27:[27]

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

Solution 28:[28]

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

Solution 29:[29]

In my case github CLI was the fix:

gh auth login

Then:

  1. GitHub.com
  2. Yes
  3. HTTPS
  4. Yes
  5. Login with a web browser