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

4 Answers 4

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.

6
  • 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. Sep 22, 2016 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, 2016 at 16:54
  • Then you might want to try using another key for authentication, I forgot how openshift works exactly, but this could be a solution.
    – MayeulC
    Sep 22, 2016 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. Feb 4, 2020 at 19:59
  • 3
    Your password is your personal access token if you have two factor auth
    – myself
    Feb 26, 2021 at 16:35
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
  • 1
    this should really be the main accepted answer now since password use has been removed from github
    – baskcat
    Mar 14 at 9: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

-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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.