Git pushing to remote GitHub repository as wrong user

后端 未结 15 813
别跟我提以往
别跟我提以往 2020-11-30 17:49

I have a work GitHub account and a personal one. First I used the personal one for test projects, then I moved on and did a repository with the other account on the same com

相关标签:
15条回答
  • 2020-11-30 17:55

    I had the same issue recently cause I created a new github account. Ive tried the answers above but it didn't help. Then I saw a post somewhere about deleting github from Keychain Access (only if you are using mac). When I git push, it then ask for username and password, and it worked!

    0 讨论(0)
  • 2020-11-30 17:58

    I would like to add - If you are working on another user's account make sure you add yourself to the collaborators area under the repositories settings.

    0 讨论(0)
  • 2020-11-30 17:59

    this sounds very similar to my current work set up. it seems that you already have set up your separate ssh-keys so you also need to create a ~/.ssh/config file and populate it with information similar to this:

    Host work.github.com
        HostName github.com
        User WORK_GITHUB_USERNAME
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/id_work_rsa
        IdentitiesOnly yes
    
    Host personal.github.com
        HostName github.com
        User PERSONAL_GITHUB_USERNAME 
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/id_personal_rsa
        IdentitiesOnly yes
    

    Every property sounds pretty self explanatory but the IdentitiesOnly one. I won't try to explain what that is for, but that is in my current setup and works fine.

    It's also worth noting that the Host URL is just a pointer to grab the correct user settings and does not have any affect on getting the files correctly to your target HostName url.

    Now you just need to make sure your origin (or any remote in general) url match the correct Host url in your respective repos depending on your user name. If you already have existing personal repos, you can edit that repo's .git/config file in your text editor:

    [remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = git@personal.github.com:PERSONAL_GITHUB_USERNAME/project.git
    

    or do it via command line:

    git remote set-url origin git@personal.github.com:PERSONAL_GITHUB_USERNAME/project.git
    

    Likewise to your work one:

    [remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = git@work.github.com:your_work_organization/project.git
    

    or again, via command line:

    git remote set-url origin git@work.github.com:your_work_organization/project.git
    

    Of course, you can always set one of your Host urls in your ~/.ssh/config file as just

    Host github.com
    

    I only used work.github.com to see the config relationships easier.

    Once these are all set, you should be able to push to each respective remote.

    EDIT

    One thing to note that I just found out myself is that if you ever set global git config values for your user.email value (and i'm guessing user.name would send a different value as well), git will show your commits as that email user. To get around this, you can override the global git config settings within your local repository:

    $ git config user.name "John Doe"
    $ git config user.email johndoe@example.com
    

    This should now send up commits as the correct user for that repo.

    0 讨论(0)
  • 2020-11-30 17:59

    This is a way to do this: you can use different ssh configurations for different ssh accounts.

    Updated on Feb 22:

    Check out this link: https://gist.github.com/2351996

    0 讨论(0)
  • 2020-11-30 18:01

    You can also just switch to https, rather than ssh. If you use https, it will respect the .git/config settings. So, in .git/config, change:

    url = git@github.com:USER/PROJECT.git

    to

    url = https://USER@github.com/USER/PROJECT.git

    (these values are on the git project page, click on the SSH and HTTP buttons to ge tthe new values);

    0 讨论(0)
  • 2020-11-30 18:01

    I have found a temporary solution in which first run killall ssh-agent then add the ssh keys generated for the account you need to use ssh-add ~/.ssh/id_4shameer

    This is the one way in which we can work on the multiple github account when we will get the error of type ERROR: Permission to user/repo-git.git denied to username.

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