git pushes with wrong user from terminal

前端 未结 22 1402
夕颜
夕颜 2020-12-07 07:51

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

相关标签:
22条回答
  • 2020-12-07 08:08

    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.

    0 讨论(0)
  • 2020-12-07 08:10

    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.

    0 讨论(0)
  • 2020-12-07 08:10

    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 git@github.com
    
    0 讨论(0)
  • 2020-12-07 08:12

    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@address.com"

    Now the commit shows the correct account.

    0 讨论(0)
  • 2020-12-07 08:14

    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.

    0 讨论(0)
  • 2020-12-07 08:16

    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.

    0 讨论(0)
提交回复
热议问题