Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git using the wrong email for push

Tags:

git

github

Okay, it's getting annoying and I can't figure out what's wrong.

I have 2 github accounts on my laptop -- for work and for personal projects.

I've set the personal email as my global configuration and then just set the work email as the git config email whenever I'm working on project for work.

My issue is that, for some odd reason, git now uses my work email for one of the personal projects when I push but when I check by running the following code in the root directory of the personal project:

git config user.email

the email that gets printed is my personal project!

The error code when I try to do a push is:

remote: Permission to personal_github_username/personal_project.git denied to work_github_username
fatal: unable to access 'https://github.com/personal_github_username/personal_project.git/': The requested URL returned error: 403

here's an additional weird thing: I can push using the Github Windows client but I can't push using the Git GUI.

At this point, I don't know what to do and I'd rather not delete the local repo and clone again from the remote repo. Any help or attempts would be highly appreciated.

like image 834
zero Avatar asked Aug 29 '18 15:08

zero


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.

What email should I use for Git?

You could use any email address. But, you must have used an email address that is connected with your GitHub account. The email address linked with Github ensures that commits are attributed to you and appear in your contributions graph.

How do I change the push origin in GitHub?

Run the git remote set-url --add --push origin git-repository-name command where git-repository-name is the URL and name of the Git repository where you want to host your code. This changes the push destination of origin to that Git repository.


3 Answers

TL;DR

You need to switch your credentials. Start with VonC's answer here.

What's going on

Note specifically the URL here:

... unable to access 'https://github.com/...'

Start with this: git push does not use your configured user.email! It uses the URL you provide, usually via a remote name like origin: there's a setting, remote.origin.url, that holds the URL.

The URL itself begins with one of the various "scheme" names, like ssh:// or https://. After the double slash is a host name—technically, this can include user:[email protected] but don't do that; at most put in [email protected]—and then the rest of the string is provided to that host. Or, you can use [email protected]:path/to/repo, which is short for ssh://[email protected]/path/to/repo.

Since you specifically asked for https, Git will get the user name and password from a credential helper. The specific credential helper that Git uses depend on a lot of things. The most important, in order, are these two:

  • What is your host OS? Windows has Windows-specific credential helpers, OS X has osx-keychain based helpers, and others have the store and cache helpers.

  • What version of Git are you using? Almost all modern versions are much better than the ancient ones, but there are still some ancient (1.7 and early 1.8) Git versions still in use.

For (much) more about credential helpers when using HTTPS, see Is there a way to skip password typing when using https:// on GitHub? I note that you added:

here's an additional weird thing: I can push using the Github Windows client but I can't push using the Git GUI.

This implies that you're using Windows (which I avoid...). Obviously their Github Windows Client is using a different credential helper than the Git GUI.

I prefer to use SSH, which means I set up my ssh keys and set my URL to use ssh://[email protected]/.... When using SSH, GitHub ends up looking up the public key that your SSH client—your Git, in other words—sends to find who is connecting, and then uses that to decide who you are and whether you can access that repository.

So where does your user.email get used?

Once your Git gets authorized and is successfully talking to another Git on GitHub, your Git then pushes commits. The commits themselves—which you've already made, at some point well before you ran git push—contain strings derived from your user.name and user.email settings.

It's git commit, not git push, that uses the user.name and user.email settings. These are just configuration settings, not credentials.

In other words, by the time you run git push, it's way past the time Git was looking at your user.email setting. What you have set now no longer matters at all. What you had set in the past matters if and only if you push some commits that used its setting.

like image 63
torek Avatar answered Oct 30 '22 01:10

torek


The answers here so far fail to mention one crucial aspect.

Namely, you did the commit, THEN changed the address setting. Needs to be the other way around for it to be recorded to the commit, before pushing it.

Therefore, after changing your email address for the repo, you have to undo and redo your commits:

git log  # to see recent history
git reset --soft HEAD~1  # to roll back
git status
git add .
git commit -m "same message"

git log 
git push
like image 43
Gringo Suave Avatar answered Oct 30 '22 01:10

Gringo Suave


Interesting. Okay lets try redoing your setup. Do the below to set your default user info for all gits.

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

Now go to your work git and ovverride the global settings using the --local tag so that for that particular project it uses your work info instead (Note: for every work git you must do this):

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

Hope that helps!

like image 32
Minutia Avatar answered Oct 30 '22 02:10

Minutia