7

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.

zero
  • 1,605
  • 3
  • 15
  • 24

3 Answers3

7

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:password@host.dom.ain but don't do that; at most put in user@host.dom.ain—and then the rest of the string is provided to that host. Or, you can use git@github.com:path/to/repo, which is short for ssh://git@github.com/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://git@github.com/.... 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.

torek
  • 448,244
  • 59
  • 642
  • 775
  • 'implies you're using Windows' HAHAHA yes. If it were up to me, I'd boot Ubuntu on this baby and go Linux. But this is my work laptop and my work requires me to work with the Microsoft stack (like Excel, Powerpoint, PowerBI, etc...) sad :( – zero Aug 29 '18 at 16:02
  • btw I'm using git 2.16. Currently reading up on credential helpers, i'll see how it goes – zero Aug 29 '18 at 16:04
  • so I switched to ssh, re-authenticated my key and I can push again. Thanks for clearing up the difference between user.email and credentials! – zero Aug 29 '18 at 16:27
6

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 the correct address to be recorded to the commit, and 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
Gringo Suave
  • 29,931
  • 6
  • 88
  • 75
  • Thank you for this addition. I'm very happy that I continued reading after following @Minutia's answer. – PfunnyGuy Feb 02 '22 at 17:40
2

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 john.doe@gmail.com

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 john.doe@work.com

Hope that helps!

rafaelasguerra
  • 2,685
  • 5
  • 24
  • 56
Minutia
  • 111
  • 6
  • The `user.name` and `user.email` settings are not *credentials*, they are merely configuration data that Git uses when making commits. – torek Aug 29 '18 at 15:57
  • Updated my answer and removed credentials. Anyways --local will guarantee that a specific user/email config after a commit for that git will be used over the --global setting, right? – Minutia Aug 29 '18 at 16:44
  • Yes, the `--local` config will override the `--global` one for that particular repository. – torek Aug 29 '18 at 18:30