51

How can I clone a repository on git with an account that's different from what I was previously using?

For example, I might have been using one account for cloning one repo, and now I need to access another repo that only a different account has access to.

Thanks for the help

David Callanan
  • 5,601
  • 7
  • 63
  • 105

4 Answers4

74

If you clone via https you can do something like

git clone https://username@github.com/username/repository.git

This will prompt you for a password.

You can also directly provide a password by calling

git clone https://username:password@github.com/username/repository.git

But be aware that the password will be saved in .git/config and your bash history in that case which is not safe. If you don't like to enter the password every time you should consider cloning via ssh.

DirkH
  • 899
  • 7
  • 9
  • I've got an ssh like this: git clone ssh://[somelongcode]@[a]-[b].rhcloud.com/~/git/[a].git/ I am using an ssh I got from openshift (a webhosting service I am using) If I don't touch it I get: Please make sure you have the correct access rights and the repository exists. – David Callanan Sep 22 '16 at 16:42
  • In your example using ssh `[somelongcode]` would be the user name. Try to replace it with the correct user name. You can also try to do a `ssh [somelongcode]@[a]-[b].rhcloud.com` to see if ssh is working. – DirkH Sep 22 '16 at 16:54
  • Then you might want to try [using another key](https://stackoverflow.com/questions/4565700/specify-private-ssh-key-to-use-when-executing-shell-command-with-or-without-ruby) for authentication, I forgot how openshift works exactly, but this could be a solution. – MayeulC Sep 22 '16 at 16:55
  • This still isn't working from terminal. I can see it and (something like 'clone') using through the Github desktop. But git clone isn't working even after entering a valid userame:password - I get "authentication failed." But the repositories do show correctly in desktop. – RichMeister Feb 04 '20 at 19:59
  • 3
    Your password is your personal access token if you have two factor auth – myself Feb 26 '21 at 16:35
  • Github removed support for password authentication on August 13, 2021. If you want to clone using HTTPS see: https://stackoverflow.com/a/73722165/5925582 – Marcelo Xavier Sep 14 '22 at 19:21
24

I tried above solution but it seems that support for password authentication was removed on August 13, 2021.

The solution is to use a personal access token which you can generate in Settings -> Developer Settings -> Personal access tokens.

After that:

git clone https://username:personal-access-token@github.com/username/repository.git
  • 1
    this should really be the main accepted answer now since password use has been removed from github – baskcat Mar 14 '23 at 09:33
1

Github removed support for password authentication on August 13, 2021. If you want to clone using HTTPS follow instructions bellow.

When you git clone, git fetch, git pull, or git push to a remote repository using HTTPS URLs on the command line, Git will ask for your GitHub username and password. When Git prompts you for your password, enter your personal access token (PAT). Password-based authentication for Git has been removed in favor of more secure authentication methods. For more information, see "Creating a personal access token." Alternatively, you can use a credential helper like Git Credential Manager.

Reference: Github page Cloning with HTTPS

Marcelo Xavier
  • 322
  • 2
  • 11
-1

If this account's ssh-key has been added to your device, you can try this after ssh-add.

git clone git@<account_username>:<repo_username>/repository.git

Hope this could help.

Venus
  • 1