How to switch git user at terminal?

52

31

I am trying to push a project to a remote repository from the command line.

From inside my local directory, I hit:

$ git push

and obtain the following error:

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

Where username1 is my github account username hosting the repository I want to push to and username2 is an old account I used to use on this machine.

I am using OS X Yosemite (v10.10.5) on a Macbook Air. And I would prefer to use https instead of ssh.

How do I update to username1 so I can successfully push to my remote?

Edit: To be clear, I am not talking about simply editing the config user object, e.g.,

$ git config --global user.name "Billy Everyteen"
$ git config --global user.email "billyeveryteen@example.com"

They have nothing to do with authentication. My question deals with user authentication necessary to write to my remote repository.

Mowzer

Posted 2016-04-11T18:29:43.100

Reputation: 1 109

Setting your username in Git – DavidPostill – 2016-04-11T18:57:25.080

Thanks. But that only deals with setting the username. Not authentication. In other words, that documentation shows us how to associate the name of who gets credit for the commits. But it doesn't actually authorize the user to push commits. – Mowzer – 2016-04-11T19:03:07.723

1

You might be able to change it user the command documented at https://git-scm.com/docs/gitcredentials . Alternatively, if you want to clear the credentials, you might look at this question http://stackoverflow.com/questions/15381198/remove-credentials-from-git .

– John – 2016-04-11T19:14:10.337

This doesn't resolve the problem! – Mihail Salari – 2019-04-05T14:41:47.350

what if I don't want to change local git settings? – Arkady – 2019-11-15T09:37:35.313

just push once to another git using another user. I wounder WHY is it so hard to do. – Arkady – 2019-11-15T09:38:09.143

Answers

49

In addition to changing username and email from terminal using git config:

$ git config --global user.name "Bob"
$ git config --global user.email "bob@example.com"

you'll need to remove authorization info from Keychain. This is something I've also struggled with until I found that I also had certificate in my Keychain.

Open up Keychain access, click on All Items and search for git. You will get some items like this:

Screenshot

Delete them. Now try to push the repo and git will ask you to write password for the user and you will be good to go.

Said Sikira

Posted 2016-04-11T18:29:43.100

Reputation: 606

1What about Windows users? – Aks.. – 2017-03-08T06:14:22.367

2i have multiple accounts what should i do – Amerrnath – 2018-01-18T08:46:42.297

I'm having the same issue with win7. I not sure what the solution is :( – Fiddle Freak – 2018-10-02T23:04:26.310

27

For cli users, just use this : git config credential.username 'Billy Everytee'

Jackman

Posted 2016-04-11T18:29:43.100

Reputation: 371

You need to disable 2FA temporarily. – bbaassssiiee – 2018-06-18T09:37:12.017

10

For Windows User:
Follow Instructions:
Control Panel >> User Account >> Credential Manager >> Windows Credential >> Generic Credential

remove git credential.
next time when you'll push repo it'll ask you for credential.
Answer reference for detailed explanation

Shubham Chadokar

Posted 2016-04-11T18:29:43.100

Reputation: 201

Thank you! That was what I was looking for on Windows. – JCF – 2019-03-25T00:39:50.267

@JCF Welcome! I am glad it helped! – Shubham Chadokar – 2019-03-25T05:23:03.160

8

List your git config.

git config --list

Change username and email global

git config --global user.name "Nanhe Kumar"
git config --global user.email "info@nanhekumar.com"

Change username and email for current repo

git config  user.name "Nanhe Kumar"
git config  user.email "info@nanhekumar.com"

Change your repo url if you are using bit bucket.

nano .git/config

This file will be something like this. [core] repositoryformatversion = 0 fileMode = false bare = false logallrefupdates = true ignorecase = true precomposeunicode = true [remote "origin"] url = https://nanhe@bitbucket.org/nanhekumar/myproject.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master

[user]
        name = Nanhe Kumar
        email = info@nanhekumar.com

Nanhe Kumar

Posted 2016-04-11T18:29:43.100

Reputation: 181

This is the most thorough answer considering global and local repo scope. – Jordan Stefanelli – 2019-10-03T17:57:50.443