How do I push to GitHub under a different username?

后端 未结 20 1918
盖世英雄少女心
盖世英雄少女心 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

提交回复
热议问题