How do I push to GitHub under a different username?
GitGithubGit Problem Overview
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
Git Solutions
Solution 1 - Git
This worked for me, it will prompt for username and password
git config --local credential.helper ""
git push origin master
Solution 2 - Git
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).
Solution 3 - Git
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
$git config --global --list
$git config --global -e
(to change account also)
Solution 4 - Git
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
Solution 5 - Git
Follow the following steps:
- 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
- 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.!
- 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
Solution 6 - 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.
Solution 7 - Git
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:
Solution 8 - Git
It's simple while cloning please take the git URL with your username.While committing it will ask your new user password.
Eg:
Solution 9 - Git
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
Solution 10 - Git
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.
Solution 11 - Git
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:
- Generate new SSH key
ssh-keygen -t rsa -C "[email protected]"
- Save it, but remember not to override the existent one
id_rsa
. Give it a different name, e.g.id_rsa_another
- 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
- Add the key to the ssh agent:
ssh-add ~/.ssh/id_rsa_another
- 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
Solution 12 - Git
Go to Credential Manager Go to Windows Credentials Delete the entries under Generic Credentials Try connecting again.
Solution 13 - Git
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
-
Set up a 2nd SSH key (like so) for your 2nd github account
-
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
Solution 14 - Git
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.
Solution 15 - Git
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.
Solution 16 - Git
I like to do it like this the best.
git push https://us[email protected]/username/repo
This will prompt for password, so you don't have to write it in plain text in the shell.
Solution 17 - Git
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.
-
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/
-
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/
-
Choose to Clone with SSH
You can push in as this user to that repo now.
Solution 18 - Git
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://[email protected]
git:https://github.com/username
- ...
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
Solution 19 - Git
If you have https://desktop.github.com/
then you can go to Preferences (or Options) -> Accounts
and then sign out and sign in.
Solution 20 - 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
"
Solution 21 - Git
Modify the file named config
located inside the .git
directory which is in the root folder of the project: [PROJECT_PATH]/.git/config
I added following section at the end of this file. After that I could push to the remote with my new name.
[user]
name = Your Name
email = [email protected]
Solution 22 - Git
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
Solution 23 - Git
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.
Solution 24 - Git
I just have included additional user on:
- repo settings,
- Manage access,
- invite a collaborator
and it worked for me.
Solution 25 - Git
I had a similar problem. I have a github account for work and a private one. On my mac I mostly use the my work github. I was not able to convince git to push to my private repo via the terminal. But it worked when I used the github desktop.
(I know there must be a different way of doing it, but none of the above answers helped, so this was the way of least resistance.)