How do I push to GitHub under a different username?

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

    As mentioned before, you can use

    git config user.name her_username
    git config user.email her_email
    

    to manually set username and email for single repo, but you have to add this command:

    git commit --amend --reset-author
    

    if you have already tried to push before the change. Otherwise the changes doesn't appear in config file.

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

    I have been using one machine to push code to two different GitHub accounts with different username. Assuming you already set up one account and want to add a new one:

    1. Generate new SSH key ssh-keygen -t rsa -C "myaddress@example.com"
    2. Save it, but remember not to override the existent one id_rsa. Give it a different name, e.g. id_rsa_another
    3. Copy the contents of the key to your GitHub account:

    Settings -> SSH and GPG keys -> New SSH key -> Give a label and paste the key -> Add SSH key

    1. Add the key to the ssh agent: ssh-add ~/.ssh/id_rsa_another
    2. Setup a GitHub host: Create a config file with touch ~/.ssh/config and edit the file by providing configurations to your accounts:
    #first account
    Host github.com-first
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa
    
    #another account
    Host github.com-another
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_another
    

    Now you should be able to push from different accounts depending on what key you add to the ssh agent, i.e. to use your first account, do ssh-add ~/.ssh/id_rsa.

    You might also want to change your user email:

    git config --global user.email "myaddress@example.com"

    or clean out ssh keys in case of permission error when pusing code to one of the accounts:

    ssh-add -D

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

    If you use different windows user, your SSH key and git settings will be independent.

    If this is not an option for you, then your friend should add your SSH key to her Github account.

    Although, previous solution will keep you pushing as yourself, but it will allow you to push into her repo. If you don't want this and work in different folder on the same pc, you can setup username and email locally inside a folder with git by removing -g flag of the config command:

    git config user.name her_username
    git config user.email her_email
    

    Alternatively, if you push over https protocol, Github will prompt for username/password every time (unless you use a password manager).

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

    Follow the following steps:

    1. You must understand that, you define author before commiting! the commits are already frozen: they have whatever name is set for their author and committer, and these cannot be changed.
    # you can check what's currently:
    git config user.name
    git config user.email
    
    git config user.name "your_github_username"
    git config user.email "your_github_email"
    
    # Again check what's currently:
    git config user.name
    git config user.email
    
    1. Check to whom your commit is tagged to?
    git log
    # once you're confirmed that it's tagged to you, then you should move to step 3
    

    In case, the author is wrong then you can easily undo last commit without losing changes

    Also, before moving to step3, don't forget to follow step one for sanity check.!

    1. give yourself a prompt to enter github_username and github_password
    git config --local credential.helper ""
    git push
    # it will ask you to enter your github_username and github_password
    
    0 讨论(0)
  • 2020-12-02 04:30

    Go to Credential Manager Go to Windows Credentials Delete the entries under Generic Credentials Try connecting again.

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

    You can add a new remote URL for the other username using git remote add origin-username https://username@github.expedia.biz/repository_name.git

    After this, if you'll push using git push -u origin-username master , this will prompt you for the password.

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