104

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 "[email protected]"

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

6
  • Setting your username in Git
    – DavidPostill
    Apr 11, 2016 at 18:57
  • 1
    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
    Apr 11, 2016 at 19:03
  • 1
    You might be able to change it user the command documented at git-scm.com/docs/gitcredentials . Alternatively, if you want to clear the credentials, you might look at this question stackoverflow.com/questions/15381198/… .
    – John
    Apr 11, 2016 at 19:14
  • This doesn't resolve the problem! Apr 5, 2019 at 14:41
  • what if I don't want to change local git settings?
    – Arkady
    Nov 15, 2019 at 9:37

8 Answers 8

92

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

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

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.

6
  • 4
    What about Windows users?
    – Aks..
    Mar 8, 2017 at 6:14
  • 13
    i have multiple accounts what should i do
    – Amerrnath
    Jan 18, 2018 at 8:46
  • I'm having the same issue with win7. I not sure what the solution is :( Oct 2, 2018 at 23:04
  • Thank you this worked perfectly
    – htafoya
    Apr 16, 2020 at 19:34
  • I used to do that but it's annoying when having to switch back and forth between accounts. This answer would be great for the question of how to make the system forget one's credentials; for changing account, I think the accepted answer should be superuser.com/a/1245296/430259.
    – Tom
    May 6, 2020 at 8:46
48

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

2
  • You need to disable 2FA temporarily. Jun 18, 2018 at 9:37
  • This wasn't enough for me. I also needed to set a different user.email using git config for the specific repo: git config user.email "[email protected]" Jul 19, 2022 at 17:06
33

List your git config.

git config --list

Change username and email global

git config --global user.name "Nanhe Kumar"
git config --global user.email "[email protected]"

Change username and email for current repo

git config  user.name "Nanhe Kumar"
git config  user.email "[email protected]"

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://[email protected]/nanhekumar/myproject.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master`

    [user]
            name = Nanhe Kumar
            email = [email protected]

4
  • 2
    This is the most thorough answer considering global and local repo scope. Oct 3, 2019 at 17:57
  • 1
    This doesn't answer the question: "My question deals with user authentication necessary to write to my remote repository." Aug 23, 2021 at 7:44
  • This is working perfectly Jul 7, 2022 at 16:35
  • @Jie thanks but this like film industry if you edit not solution indentation content your earn point so i have long time not answring Jul 7, 2022 at 22:05
28

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

2
  • 1
    Thank you! That was what I was looking for on Windows.
    – JCF
    Mar 25, 2019 at 0:39
  • @JCF Welcome! I am glad it helped! Mar 25, 2019 at 5:23
8

Other plausible option, if you wanted to use the "new user" on only one project, you can do it by configuring it just for the project's directory in which you are working. e.g:

 git config --local user.name "Mike"
 git config --local user.email "[email protected]"

note that I'm using --local instead of --global.

2
2

Solving the actual authentication issue, one needs to specify the correct remote, if the standard git config suggestions don't work.

If so, you're probably using an SSH key and it's in your keychain. When using a repository with a new user, make sure your SSH key is configured in ~/.ssh/config (handy link to instructions if you need to add a new key), like so:

Host github.com
   HostName github.com
   IdentityFile ~/.ssh/myFirstAccountKey
   IdentitiesOnly yes

Host github-second-account
   HostName github.com
   IdentityFile ~/.ssh/mySecondAccountKey
   IdentitiesOnly yes

I'm not sure if the IdentitiesOnly attribute is required for either of the two.

Then update the remote origin of the repository you're having auth issues to use the second account:

git remote add origin git@github-second-account:[YOUR_GITHUB_USERNAME]/[YOUR_GITHUB_REPO].git

See for more detail: https://gist.github.com/oanhnn/80a89405ab9023894df7

1

If you're on windows you can simply use GitManager (GitAccountManager on npm): https://github.com/paul-hanneforth/GitManager. There you can do it all with only one command.

1
  • 2
    Although it's reasonably obvious from the link & your user name, to prevent being potentially flagged as spam it's always better to make your affiliation clear in your post.
    – Tetsujin
    Mar 30, 2020 at 12:43
0

i was having the same problem. What i did was to remove the ssh key i had in my other account.

To explain in a better way. I have 2 accounts in the same computer and both accounts I have ssh keys, so it was confliting in the moment i tried to push in the newest account I created. So i logged in the oldest account and deleted the ssh key, then I tried pushing in my newest account and it worked.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .