2

Is there a quick way to just log out and log back in with a different account on git in cmd on windows? I tried:

git config --global --unset user.name
git config --global --unset user.mail       
git config --global --unset credential.helper

so I understand I should have been logged out with the old_user_name? Then I used:

git config --global user.name "new_user_name"
git config --global user.email "new_email"
git push origin master

I got a massage:

remote: Permission to new_user_name/new_repo.git denied to old_user_name. fatal: unable to access 'https://github.com/new_user_name/new_repo.git/': The requested URL returned error: 403

That means I'm still logged in with the old account. What else can I do?

lukasz21
  • 59
  • 5

1 Answers1

5

The user.name and user.email settings are not logins, in any way, shape, or form. (The credential.helper setting is useful here, but just unsetting it is probably not right.)

Whenever you make a new commit (by, e.g., running git commit), Git needs to know what user name and email address to put in the new commit. Git gets these by reading user.name and user.email at that time. That's all they're for: to set these in new commits.1

When you run git push, you do (usually2) need to authenticate in order to send your new commits to some other Git repository. Git does not do this authentication. Git relies on other programs to do it. Those other programs include:

  • Web servers, via https URLs: here, Git has to send a user name and password or PAT or some other secret to the web server. Git gets these from:

    1. the URL, if they're in the URL;
    2. a credential helper, which you may configure: Windows Git comes with a Windows-specific credential helper, macOS Git comes with a macOS-specific credential helper, and so on; or
    3. as a last resort, reading the user name and password from the keyboard.

    Generally the right way to do this is with a credential helper. See the gitcredentials documentation and the specific credential helpers available for your particular Git installation. The problem with method #1 is that the password is visible right there in the URL by anyone looking at your computer; the problem with method #3 is obvious.

    Unsetting the credential helper gets you the default for your system. This might not be the best thing to do. For Windows systems, see Remove credentials from Git and in particular VonC's answer here.

  • Secure Shell (SSH): here, Git simply runs the ssh command. Windows systems come with their own ssh command these days, and Git-for-Windows comes with its own ssh command because the older Windows systems either lacked ssh entirely or provided an inadequate version, so on Windows systems, you must make sure that you configure and use the correct ssh (whatever that may be for your installation).

Since you are using an https:// URL, you will need to set up your credential system. If you wish to use ssh:// URLs, set up ssh.


1Note that git rebase works by copying existing commits to new and (supposedly / intended-to-be) improved ones, so this too uses the settings: it's making new commits.

2One can set up a completely open, anonymous network where anyone can push anything at any time, but these are way too easy to abuse, so nobody does this in practice.

torek
  • 448,244
  • 59
  • 642
  • 775