Search code examples
gitpush

Unable to push to remote repo in a directory with local git configuration


I have a special directory which has a local git configuration that is different from the user git configuration. See the following screenshot: enter image description here

As you can see, the user name is title-case and the user email is 18871315325@163.com. However, when I use git push origin main to push datas, an error occurs ERROR: Permission to title-case/title-case.github.io.git denied to SwitWu. fatal: Could not read from remote repository.

I'm not sure whether I have provided enough information. If not, please tell me. Any helps are appreciated.


Solution

  • The user.name and user.email fields there have nothing whatsoever to do with authentication to GitHub, GitLab, or any other remote host. Those fields are only there to make sure that commits are attributed to the right person. You can change them every time you push or pull or clone, and it won't affect the outcome of your push or pull or clone.

    You're getting that message because your SSH connection to GitHub is happening using the key that GitHub associates with its SwitWu account. If you want SwitWu to be able to push and pull on that repo, then grant that user that permission; otherwise, to push as title-case using SSH, then you'll need to use a different key associated with the title-case account.

    To set up multiple SSH keys for multiple GitHub (or GitLab, etc.) accounts,

    1. Create the new key, or use one that is not already associated with another account
    2. Add the public key to the account or repo, as appropriate
    3. Update ~/.ssh/config in your favorite editor:
    Host github.com
      IdentityFile ~/.ssh/id_ed25519_userA # path to your main account's key
      IdentitiesOnly yes
    
    Host userB.github.com
      IdentityFile ~/.ssh/id_ed25519_userB # path to your second account's key
      IdentitiesOnly yes
    
    # repeat for as many keys/identities as you like, and name the `Host` and key files whatever works best for you
    
    1. If you already have repos that should use userB, then git remote set-url origin git@userB.github.com:owner/repo.git from those repos.
    2. If you need to clone repos as userB, then git clone git@userB.github.com:owner/repo.git

    Pushes, pulls, and other Git commands that reference the network will only know the remote as origin (or whatever other name you give it), so once this is done then SSH will handle the different credentials behind the scenes.