32

I need to change who git thinks I am so I can push to a different repo ( both are mine. ).

Here is a similar issue but I don't want to set any config variables.

I just want to login once to my current username. I plan on deleting my other account.

Here is the error when I attempt a git push origin master

remote: Permission to current_user/fav-front.git denied to user_to_delete. fatal: unable to access 'https://github.com/current_user/repo.git/': The requested URL returned error: 403

Please note that the git config variables for user and email ( user.name and user.email ) are not related to the authentication that git push uses.

They are used for commits.

To reiterate, I want to login to git as current user.

  • 2
    What about this answer to the question: https://stackoverflow.com/a/46337214/10871900 You don't need to include the password: `https://user@github.com/username/repo.git` – dan1st Jun 13 '19 at 04:50
  • Nope. I saw that. I simply want to login to git like I did with the old account. After this single login git will remember that username / password and I won't have to fiddle with it any more. –  Jun 13 '19 at 04:52
  • If you want to do it only once, you can do `git push https://user@github.com/username/repo.git master` – dan1st Jun 13 '19 at 04:54
  • I want git to think/know that I am user_new and not user_old. –  Jun 13 '19 at 04:56
  • Do you want git to think that only for one push or one commit or forever(in that repo)? – dan1st Jun 13 '19 at 04:57
  • I had to login through bash before as it prompted me to ... is this impossible to ever do again or something? –  Jun 13 '19 at 04:57
  • Commits are not even involved as they are local operations. I want git to think this for all my pushes as I mentioned I am deleting my older account. –  Jun 13 '19 at 04:58
  • Do you mean that git saved your credentials and you want to undo it? – dan1st Jun 13 '19 at 04:58
  • That appears to be what happened but I'm not sure. –  Jun 13 '19 at 04:59
  • What is your credential helper? – dan1st Jun 13 '19 at 04:59
  • 2
    I feel like I'm asking how to build a spaceship. I just want to login to git. Git must be black magic as I have seen no Google hit on how to do something like `git login` –  Jun 13 '19 at 05:00
  • I don't know what that is. I never installed a credential helper. –  Jun 13 '19 at 05:00
  • I'm not using SSH ... I'm using https ... how the fuck does git know who I am. Who stored my credentials is a good question. –  Jun 13 '19 at 05:02
  • Found this ... https://help.github.com/en/articles/caching-your-github-password-in-git –  Jun 13 '19 at 05:03
  • I never did this unless a monkey got on my machine and did it for me when I wasn't looking. –  Jun 13 '19 at 05:04
  • Found this ...https://help.github.com/en/articles/updating-credentials-from-the-osx-keychain –  Jun 13 '19 at 05:05
  • The keychain app has my username and password. That should be illegal. I never gave it permission or set it up. –  Jun 13 '19 at 05:09
  • What is a config? –  Jun 13 '19 at 05:09
  • I had internet problems and I didn't see a few comments. It was about finding out that with keychain. – dan1st Jun 13 '19 at 05:11
  • And configs are configurations for git(for your repository, your user or your pc) – dan1st Jun 13 '19 at 05:12
  • @user11623870 Yes, I feel its hard time we have something like `git login` or atleast `github login` ! – Rajesh Swarnkar Feb 27 '23 at 04:24
  • @RajeshSwarnkar You do have that, for some repository hosting services. I have [edited my answer](https://stackoverflow.com/a/56573696/6309) accordingly. – VonC Feb 27 '23 at 07:40
  • 1
    @VonC That's impressive (although the cli guided setup was little awkward). And yes, I had to install the [Github CLI](https://cli.github.com/). Take my upvote. – Rajesh Swarnkar Feb 27 '23 at 07:50

3 Answers3

20

I had a similar problem with Windows. Updating Credentials Manager helped in my case.

To open Credentials Manager search that setting or navigate to: Control Panel\All Control Panel Items\Credential Manager. In Windows Credentials -> Generic Credentials find your repo and update username/password or delete all that are not needed.

karel
  • 5,489
  • 46
  • 45
  • 50
pbaranski
  • 22,778
  • 19
  • 100
  • 117
17

You don't login to Git.

You do login to a Git repository hosting server, which request an authentication, but Git itself has no authentication nor authorization.

(As an example of Git repository hosting service offering login:

What Git does have is credential caching (check the output of git config credential helper).
On Mac: "Updating credentials from the OSX Keychain": you can check if your old user was stored there, and update it.

If you really want to disable the credential helper, you will be asked your credentials every time you push to a repository hosted on a server requesting authentication.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    How did keychain get my permission to save my username and password? –  Jun 13 '19 at 05:10
  • @DaraKhosrowshahi It prompted for them once, then cache them there. – VonC Jun 13 '19 at 05:10
  • 6
    I know but it never asked me. It, git, prompted for them once cached them and locked it down like a f****** rabbit hole and stole 2 hours of my life. –  Jun 13 '19 at 05:11
  • This is the credential input from keychain and not from git. – dan1st Jun 13 '19 at 05:14
  • @VonC - I just deleted it from the keychain. This prompted git to ask for username and password. –  Jun 13 '19 at 05:15
  • After re logging in, it put it back in the keychain as the correct user. Is this default behavior for the keychain app? –  Jun 13 '19 at 05:16
  • @dan1st Right, I have deleted the comment – VonC Jun 13 '19 at 05:16
  • @DaraKhosrowshahi Yes, because of `git config credential.helper`: it is Git which does instruct the keychain to store your credentials, not the keychain itself. – VonC Jun 13 '19 at 05:17
  • Well I never touched this thing ... I need to read the docs on git config credential.helper ... I have been traumatized by the keychain app. I am going to sue Apple for emotional distress :) –  Jun 13 '19 at 05:18
  • Trick is to just delete the entry and this will force it to ask you to login again. What I want to know, IS THIS DEFAULT BEHAVIOR? –  Jun 13 '19 at 05:20
  • @DaraKhosrowshahi It is the default behavior to ask (Git or not: if the remote URL demans an authentication, it will ask for it), but the storing part is the result of the credential helper. You can disable it with `git config --global --unset credential.helper` – VonC Jun 13 '19 at 05:22
  • So the credential helper, in this case, keychain, is set to this behavior by default? –  Jun 13 '19 at 05:23
  • @DaraKhosrowshahi the keychain is set to this behavior by Git. The keychain alone would not register anything unless explicitly instructed to do so. Here, Git instructs the keychain to do so because of the credential.helper settings. – VonC Jun 13 '19 at 05:24
  • Yea, I know, I got that, a while back. But what is the default setting for git, is it set to do this by default if I say do a fresh git install ( all default behavior ). Will test tomorrow unless you know. –  Jun 13 '19 at 05:27
  • @DaraKhosrowshahi it should not be set by default, but the result of a manual configuration: https://help.github.com/en/articles/caching-your-github-password-in-git – VonC Jun 13 '19 at 05:29
  • I find it a bit creepy that it does this be default, kind of like a web app remembering you logged in with out asking. Guess that is the standard now. There used to be "remember login" box you checked when logging in but not any more. –  Jun 13 '19 at 05:37
  • @DaraKhosrowshahi I am not sure about the "default" part, as I believe you still need to configure Git to trigger that credential caching. But yes, that can be unsettling. – VonC Jun 13 '19 at 05:41
-3

I just tried to clone a repo from my github profile

then the popup of github login showed up

it has the options to login through browser or token. I clicked on browser option then it got authorized

Skidee
  • 563
  • 5
  • 8