[git] How do I push to GitHub under a different username?

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 her to push to her account. But it keeps trying to use my username and saying I don't have access to her repository:

$ git push her_github_repository our_branch
ERROR: Permission to her_username/repository.git denied to my_username.
fatal: The remote end hung up unexpectedly

This question is related to git github

The answer is


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

git config --local credential.helper ""
git push origin master

It's simple while cloning please take the git URL with your username.While committing it will ask your new user password.

Eg:

git clone https://[email protected]/username/reponame.git

git clone https://[email protected]/username/reponame.git


this, should work: git push origin local-name:remote-name

Better, for GitLab I use a second "origin", say "origin2":

git remote add origin2 ...
then

git push origin2 master

The conventional (short) git push should work implicitly as with the 1st "origin"


The userid where the commit happens is stored in the config file.

go to the top of the repository vi .git/config

change the url line listed after "[remote "origin"] to have the appropriate userid


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
[email protected]: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
[email protected]:either/branch/or/path

$ git config remote.origin.url [email protected]:either/branch/or/path

$ git push
[email protected]:either/branch/or/path's password:

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

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


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:

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

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


I couldn't figure out how to have a 2nd github identity on the one machine (none of these answers worked for me for that), but I did figure out how to be able to push to multiple different github accounts as myself.

Push as same username, but to different github accounts

  1. Set up a 2nd SSH key (like so) for your 2nd github account

  2. Change between accounts thus :

Push with my new 2nd github account

ssh-add -D
ssh-add ~/.ssh/ssh_key_for_my_2nd_account
git push

Push with my main account

ssh-add -D
ssh-add ~/.ssh/id_rsa   
git push

If you have https://desktop.github.com/
then you can go to Preferences (or Options) -> Accounts
and then sign out and sign in.


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.


You can add a new remote URL for the other username using git remote add origin-username https://[email protected]/repository_name.git

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


I setup an ssh alias using a custom IdentityFile and rewrote the origin to use my custom me-github hostname.

#when prompted enter `id_rsa_user1` as filename
ssh-keygen -t rsa

# ~/.ssh/config
Host user1-github
HostName github.com
Port 22
User git
IdentityFile ~/.ssh/id_rsa_user1

#check original remote origin url
git remote -v
origin [email protected]:user1/my-repo.git

#change it to use your custom `user1-github` hostname
git remote rm origin
git remote add origin git@user1-github:user1/my-repo.git

If you use an SSH URL ([email protected]:username/projectname.git) rather than an HTTPS URL, you can temporarily specify a different SSH key using the $GIT_SSH_COMMAND environment variable:

$ GIT_SSH_COMMAND="ssh -i different_private_key" git push

As far as I can tell, with SSH URLs, GitHub doesn't care about user names, only about keys: if a user account has access to a repository, and that account has an SSH key (see the SSH keys page in the account settings), then if you use that SSH key to push to that repository, the push will be treated as coming from that user.


git add .
git commit -m "initial commit"
git config --local credential.helper ""
git push https://github.com/youraccount/repo.git --all

After this push command, a username password prompt will be opened.


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 "[email protected]"

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


I just have included additional user on:

  • repo settings,
  • Manage access,
  • invite a collaborator

and it worked for me.


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.


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 "[email protected]"
  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 "[email protected]"

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

ssh-add -D


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)

git config user.name only changes the name I commit. I still cannot push. This is how I solved it, and I think is an easy way to me.

  1. Generate a SSH key under the user name you want to push on the computer you will use https://help.github.com/articles/connecting-to-github-with-ssh/

  2. Add this key to the github user account that you want to push to https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/

  3. Choose to Clone with SSH

You can push in as this user to that repo now.