David Callanan
David Callanan

Reputation: 5810

git clone with different username/account

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

Upvotes: 59

Views: 131395

Answers (5)

Yash Mochi
Yash Mochi

Reputation: 967

If you have multiple account with same version control system, you can setup your ssh config file to use different identity files as below:

For ex: You have created two identities in your .ssh folder (Windows: C:\Users\{yourUserName}\.ssh, Linux/MAC: ~/. ssh) with below names,

  • 'personal' (for personal bitbucket, github or any other account) and
  • 'work' (for your office bitbucket, github or any other account)

You can update your config file in .ssh folder like below,

# Global options
Host *
  # Always only use specified identities, do not fall back to
  # everything found in ssh-agent.
  IdentitiesOnly yes

# Repeat following for each context
## When in "personal" context directory, use "personal" ssh key for bitbucket.org or github.com
Host personal
    HostName bitbucket.org
    IdentityFile ~/.ssh/personal

## When in "work" context directory, use "work" ssh key for bitbucket.org or github.com
### Of course, other hosts that require this context
### can be added as well, just using the host name
Host bitbucket.org
 IdentityFile ~/.ssh/work

And once you clone any project from personal version control system (bitbucket or github or anything else),

For Personal:

make sure to use 'personal' as host name.

git clone git@personal:mypersonalworkspace/myproject.git

instead of

git clone [email protected]:mypersonalworkspace/myproject.git

For Work:

For work project you can continue using

git clone [email protected]:myofficeworkspace/myproject.git

Upvotes: 5

Marcelo Xavier
Marcelo Xavier

Reputation: 366

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

Upvotes: 1

Venus
Venus

Reputation: 11

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.

Upvotes: 0

Juani D&#39;Ambrosio
Juani D&#39;Ambrosio

Reputation: 351

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:[email protected]/username/repository.git

Upvotes: 31

DirkH
DirkH

Reputation: 978

If you clone via https you can do something like

git clone https://[email protected]/username/repository.git

This will prompt you for a password.

You can also directly provide a password by calling

git clone https://username:[email protected]/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.

Upvotes: 82

Related Questions