How do I push to GitHub under a different username?

后端 未结 20 1922
盖世英雄少女心
盖世英雄少女心 2020-12-02 04:00

A friend and myself are sharing my computer. I\'ve made pushes to GitHub using the git bash shell on Windows 7. Now we\'re in a different project on that computer and I need

相关标签:
20条回答
  • 2020-12-02 04:32

    If under Windows and user Git for Windows and the manager for managing the credentials (aka Git-Credential-Manager-for-Windows Link) the problem is that there is no easy way to switch amongst users when pushing to GitHub over https using OAuth tokens.

    The reason is that the token is stored as:

    • Internet Address: git:https://github.com
    • Username: Personal Access Token
    • Password: OAuth_Token

    Variations of the URL in Internet Address don't work, for example:

    • git:https://username@github.com
    • git:https://github.com/username
    • ...

    The solution: namespaces. This is found in the details for the configuration of the Git-Credential-Manager-for-Windows:

    • https://github.com/Microsoft/Git-Credential-Manager-for-Windows/blob/master/Docs/Configuration.md

    Quoting from it:

    namespace

    Sets the namespace for stored credentials.

    By default the GCM uses the 'git' namespace for all stored credentials, setting this configuration value allows for control of the namespace used globally, or per host.

    git config --global credential.namespace name
    

    Now, store your credential in the Windows Credential Manager as:

    • Internet Address: git.username:https://github.com
    • Username: Personal Access Token
    • Password: OAuth_Token

    Notice that we have changed: git -> git.username (where you change username to your actual username or for the sake of it, to whatever you may want as unique identifier)

    Now, inside the repository where you want to use the specific entry, execute:

    git config credential.namespace git.username
    

    (Again ... replace username with your desired value)

    Your .git/config will now contain:

    [credential]
        namespace = git.username
    

    Et voilá! The right credential will be pulled from the Windows Credential Store.

    This, of course, doesn't change which user/e-mail is pushing. For that you have to configure the usual user.name and user.email

    0 讨论(0)
  • 2020-12-02 04:32

    If you use ssh and get

    Permission to some_username/repository.git denied to Alice_username

    while you don't wanna push as Alice_username, make sure Alice_username doesn't have your computer's ssh key added to its github account.

    I deleted my ssh key from alice's github account and the push worked.

    0 讨论(0)
  • 2020-12-02 04:33

    You can push with using different account. For example, if your account is A which is stored in .gitconfig and you want to use account B which is the owner of the repo you want to push.

    Account B: B_user_name, B_password
    Example of SSH link: https://github.com/B_user_name/project.git

    The push with B account is:

    $ git push https://'B_user_name':'B_password'@github.com/B_user_name/project.git
    

    To see the account in .gitconfig

    1. $git config --global --list
    2. $git config --global -e (to change account also)
    0 讨论(0)
  • 2020-12-02 04:36

    if this is your problem

    remote: Permission to username1/repo.git denied to username2.
    fatal: unable to access 'https://github.com/username1/repo.git/':
    The requested URL returned error: 403
    

    In addition to changing username and email from terminal using git config:

    $ git config --global user.name "Bob"
    $ git config --global user.email "bob@example.com"
    

    you'll need to remove authorization info from Keychain. This solution took me several hours to figure out.I found that I also had certificate in my Keychain.

    Open up Keychain access, click on All Items and search for git. Delete all keychain

    0 讨论(0)
  • 2020-12-02 04:38

    This worked for me, it will prompt for username and password

    git config --local credential.helper ""
    git push origin master
    
    0 讨论(0)
  • 2020-12-02 04:39

    If after running git push Git asks for a password of user, but you would like to push as new_user, you may want to use git config remote.origin.url:

    $ git push
    user@my.host.com:either/branch/or/path's password:
    

    At this point use ^C to quit git push and use following to push as new_user.

    $ git config remote.origin.url
    user@my.host.com:either/branch/or/path
    
    $ git config remote.origin.url new_user@my.host.com:either/branch/or/path
    
    $ git push
    new_user@my.host.com:either/branch/or/path's password:
    
    0 讨论(0)
提交回复
热议问题