How to switch git user at terminal?

469,683

Solution 1

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.

Solution 2

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

Solution 3

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]

Solution 4

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

Solution 5

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.

Share:
469,683

Related videos on Youtube

Mowzer
Author by

Mowzer

Just here to learn and grow as a coder/developer. I appreciate all the knowledge others share and I try to help out where I can. I am very thankful for your wisdom and grateful that you are willing to share it. I always ACCEPT and UPVOTE answers! Here's proof! My formula for writing questions. State your goal. Describe the behavior you expect to see. Describe what you actually see. List the detailed steps to recreate the problem. (Optional) Describe any solutions you might have tried or areas where you think the problem might be. Ask the actual question itself. (Highlighting optional). Show your code. (Preferably in a demo like jsBin, plunkr, jsFiddle or CodePen.) See this example SO question.

Updated on September 18, 2022

Comments

  • Mowzer
    Mowzer over 1 year

    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.

    • Admin
      Admin about 8 years
    • Admin
      Admin about 8 years
      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.
    • Admin
      Admin about 8 years
      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/… .
    • Admin
      Admin about 5 years
      This doesn't resolve the problem!
    • Admin
      Admin over 4 years
      what if I don't want to change local git settings?
    • Admin
      Admin over 4 years
      just push once to another git using another user. I wounder WHY is it so hard to do.
  • Aks..
    Aks.. about 7 years
    What about Windows users?
  • Amerrnath
    Amerrnath over 6 years
    i have multiple accounts what should i do
  • bbaassssiiee
    bbaassssiiee almost 6 years
    You need to disable 2FA temporarily.
  • Fiddle Freak
    Fiddle Freak over 5 years
    I'm having the same issue with win7. I not sure what the solution is :(
  • JCF
    JCF about 5 years
    Thank you! That was what I was looking for on Windows.
  • Shubham Chadokar
    Shubham Chadokar about 5 years
    @JCF Welcome! I am glad it helped!
  • Jordan Stefanelli
    Jordan Stefanelli over 4 years
    This is the most thorough answer considering global and local repo scope.
  • Tetsujin
    Tetsujin about 4 years
    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.
  • htafoya
    htafoya about 4 years
    Thank you this worked perfectly
  • Tom
    Tom almost 4 years
    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.
  • MiB
    MiB about 3 years
    Doesn't simply omitting --global create the same effect as "Git will write to a local level if no configuration option is passed"?
  • Will Manley
    Will Manley over 2 years
    This doesn't answer the question: "My question deals with user authentication necessary to write to my remote repository."
  • Will Manley
    Will Manley over 2 years
    This doesn't answer the question: "My question deals with user authentication necessary to write to my remote repository."
  • Brunox13
    Brunox13 over 2 years
    In case you're having trouble deleting the password after a search: apple.stackexchange.com/a/344380/350595
  • Admin
    Admin almost 2 years
    This is working perfectly
  • Admin
    Admin almost 2 years
    @Jie thanks but this like film industry if you edit not solution indentation content your earn point so i have long time not answring