40

How can I clone something on git with a different account?

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

Thanks for the help

David Callanan
  • 4,570
  • 4
  • 54
  • 90

3 Answers3

64

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
  • 799
  • 6
  • 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
  • 2
    Your password is your personal access token if you have two factor auth – myself Feb 26 '21 at 16:35
10

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
0

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