Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git pushing to remote GitHub repository as wrong user

I have a work GitHub account and a personal one. First I used the personal one for test projects, then I moved on and did a repository with the other account on the same computer.

Now I wanted to create a new repository on my personal account again, I changed the global and local user.name, and did a new ssh key pair, entered in the GitHub setup page. Then I set up the directory

git init git remote add origin <url> git push origin 

but that now tells me

ERROR: Permission to personaluser/newrepo.git denied to

I have no idea how the other account is connected to this one. .git/config shows no workusername related things.

If you're using Windows 10 take your time to read the Rajan's answer.

like image 347
Raz Faz Avatar asked Jan 12 '11 04:01

Raz Faz


People also ask

Why are my commits linked to the wrong user?

GitHub uses the email address in the commit header to link the commit to a GitHub user. If your commits are being linked to another user, or not linked to a user at all, you may need to change your local Git configuration settings, add an email address to your account email settings, or do both.

How do I push to GitHub under a different username?

As stated in the other answers, a good way is to alter the remote url: git push https://[email protected]/username/provided path. git/ where everything after the @ is the default https url of the repo. Show activity on this post. You can push with using different account.

How do I switch users on GitHub?

In the upper-right corner of any page, click your profile photo, then click Settings. In the left sidebar, click Account. In the "Change username" section, click Change username.


1 Answers

this sounds very similar to my current work set up. it seems that you already have set up your separate ssh-keys so you also need to create a ~/.ssh/config file and populate it with information similar to this:

Host work.github.com     HostName github.com     User WORK_GITHUB_USERNAME     PreferredAuthentications publickey     IdentityFile ~/.ssh/id_work_rsa     IdentitiesOnly yes  Host personal.github.com     HostName github.com     User PERSONAL_GITHUB_USERNAME      PreferredAuthentications publickey     IdentityFile ~/.ssh/id_personal_rsa     IdentitiesOnly yes 

Every property sounds pretty self explanatory but the IdentitiesOnly one. I won't try to explain what that is for, but that is in my current setup and works fine.

It's also worth noting that the Host URL is just a pointer to grab the correct user settings and does not have any affect on getting the files correctly to your target HostName url.

Now you just need to make sure your origin (or any remote in general) url match the correct Host url in your respective repos depending on your user name. If you already have existing personal repos, you can edit that repo's .git/config file in your text editor:

[remote "origin"]     fetch = +refs/heads/*:refs/remotes/origin/*     url = [email protected]:PERSONAL_GITHUB_USERNAME/project.git 

or do it via command line:

git remote set-url origin [email protected]:PERSONAL_GITHUB_USERNAME/project.git 

Likewise to your work one:

[remote "origin"]     fetch = +refs/heads/*:refs/remotes/origin/*     url = [email protected]:your_work_organization/project.git 

or again, via command line:

git remote set-url origin [email protected]:your_work_organization/project.git 

Of course, you can always set one of your Host urls in your ~/.ssh/config file as just

Host github.com 

I only used work.github.com to see the config relationships easier.

Once these are all set, you should be able to push to each respective remote.

EDIT

One thing to note that I just found out myself is that if you ever set global git config values for your user.email value (and i'm guessing user.name would send a different value as well), git will show your commits as that email user. To get around this, you can override the global git config settings within your local repository:

$ git config user.name "John Doe" $ git config user.email [email protected] 

This should now send up commits as the correct user for that repo.

like image 133
hellatan Avatar answered Sep 18 '22 19:09

hellatan