31

I am trying to sign in using the Git command line, so that I can push my changes to a repository I have. I usually use a different account to the one I'm trying to use right now, and pushing works fine on there. The terminal is the one integrated into Visual Studio Code, and it is Git Bash.

What I want to do is sign into this different account and then push from that account. I remember at one point there was a popup with a login box on it. How would I get this to show up again?

So far, I have tried using the commands:

git config user.name my-name
git config user.email my-email

Which don't appear to have asked for any credentials. Upon Googling (just now) it appears that they are used just to set my commit username.

So how do I log in from the Git command line? I've been stuck on this for ages and I feel like there's probably a really simple solution.

Any help would be greatly appreciated.

wallisers
  • 388
  • 3
  • 16
William Jones
  • 809
  • 2
  • 11
  • 29
  • In github or VSTS you need to generate token and you will use it as password.your email address will be username. – Anirudha Gupta Sep 15 '19 at 18:57
  • @AnirudhaGupta Is that an SSH key? I was trying to use HTTPS but if that's easier then I'll just do that. – William Jones Sep 15 '19 at 19:00
  • You are not specifying on which host you are trying to push. Git is just a _stupid content tracker_, It looks like your issue has more to do with something like BitBucket, Github, GitLab... rather than Git. – Ulysse BN Sep 15 '19 at 19:10
  • I did use same method in windows and mac, it's adding SSH key itself for me. Try it, it might work for you as well. – Anirudha Gupta Sep 15 '19 at 19:10
  • As you mention in Q that it's not telling for password, clone the repos and it will tell you to enter authentication. – Anirudha Gupta Sep 15 '19 at 19:11

5 Answers5

24

Just entering your user in the command line won't work for newer versions of git. To sign in, you'll need to download git CLI, then do gh auth login

user18004704
  • 259
  • 2
  • 6
  • 1
    Do `git push` and `git pull` operations use the `gh auth login` you have done? I thought this would only be effective for future `gh` operations, not for future `git` operations. – joanis Jan 26 '22 at 14:37
  • Yes, if you use ```gh auth login``` it will login to your account and you will be able to use git with your account as well. I tried it. I use this method all the time as I have to push and pull to my private repos – user18004704 Jan 28 '22 at 15:34
  • Oh, that is super cool. I will have to test that feature. – joanis Jan 28 '22 at 16:44
7

user.name & user.email have nothing to do with the server communication - they are used only when committing. If you use HTTP to access your repo, then add another remote with your new username in the URL:

git remote add origin2 https://new_username@host.com/repo

And then push/fetch using the new remote:

git push origin2 branch_name

Windows will recognize that the URL has new username and will ask for your creds.

You can also use SSH. Generate the key, upload the public one to your Git Server under the right account, set up .ssh/config to use that key. Though you still will need either to add new remote or re-point the existing one to switch to SSH protocol.

Git Credential Manager

Another option (as stated here) that's available since 2018 is to git credential-manager erase <url>. Though personally I didn't test it.

Stanislav Bashkyrtsev
  • 14,470
  • 7
  • 42
  • 45
  • `git credential-manager reject` would actually sign the user **out**, but it has since been changed to `erase`. Documentation about command line usage is available on the project's [github page](https://github.com/git-ecosystem/git-credential-manager/blob/release/docs/usage.md). – lolfail Mar 08 '23 at 17:31
  • Updated to `erase` – Stanislav Bashkyrtsev Mar 09 '23 at 07:16
4
  1. To authenticate on Windows, use this command to install GitHub CLI:

    winget install --id GitHub.cli

For more information on other OS, visit https://github.com/cli/cli#installation

  1. After installation, you may have to restart your code editor if you are using its terminal. Then,

    gh auth login

Supply the necessary info and you're logged in. This worked for me

4

Yes, just winget install --id GitHub.cli and gh auth login worked, but you have to close your terminal and re-open to try again.

Harsh
  • 812
  • 1
  • 10
  • 23
2

Here is the link to the documentation that explains how to login to github using a variety of methods.
https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/about-authentication-to-github

If you are using a Mac you may also have to remove any cached credentials. https://docs.github.com/en/get-started/getting-started-with-git/updating-credentials-from-the-macos-keychain

Carlos Ferreira
  • 1,980
  • 2
  • 14
  • 18