I know how to provide a username and password to an https request like this:
git clone https://username:password@remote
But I'd like to know how to provide a username and password to the remote like this:
git clone git@remote.git
I've tried like this:
git clone username:password@git@remote.git
git clone git@username:password@remote.git
git clone git@remote.git@username:password
But they haven't worked.
This way worked for me from a github repository
Edit based on Michael_Scharf's comment:
You can leave out the password so that it won't be logged your bash history file:
It will prompt you for your password
The
user@host:path/to/repo
format tells Git to use ssh to log in tohost
with usernameuser
. Fromgit help clone
:The part before the
@
is the username, and the authentication method (password, public key, etc.) is determined by ssh, not Git. Git has no way to pass a password to ssh, because ssh might not even use a password depending on the configuration of the remote server.Use
ssh-agent
to avoid typing passwords all the timeIf you don't want to type your ssh password all the time, the typical solution is to generate a public/private key pair, put the public key in your
~/.ssh/authorized_keys
file on the remote server, and load your private key intossh-agent
. Also see Configuring Git over SSH to login once, GitHub's help page on ssh key passphrases, gitolite's ssh documentation, and Heroku's ssh keys documentation.Choosing between multiple accounts at GitHub (or Heroku or...)
If you have multiple accounts at a place like GitHub or Heroku, you'll have multiple ssh keys (at least one per account). To pick which account you want to log in as, you have to tell ssh which private key to use.
For example, suppose you had two GitHub accounts:
foo
andbar
. Your ssh key forfoo
is~/.ssh/foo_github_id
and your ssh key forbar
is~/.ssh/bar_github_id
. You want to accessgit@github.com:foo/foo.git
with yourfoo
account andgit@github.com:bar/bar.git
with yourbar
account. You would add the following to your~/.ssh/config
:You would then clone the two repositories as follows:
Avoiding ssh altogether
Some services provide HTTP access as an alternative to ssh:
GitHub:
Gitorious:
Heroku: See this support article.
WARNING: Adding your password to the clone URL will cause Git to store your plaintext password in
.git/config
. To securely store your password when using HTTP, use a credential helper. For example:The above will cause Git to ask for your password once every 15 minutes (by default). See
git help credentials
for details.On Windows, the following steps should re-trigger the GitHub login window when
git clone
ing:I solved this problem in the following way:
Run this first before cloning the same way, should be fixed!
In the comments of @Bassetassen's answer, @plosco mentioned that you can use
git clone https://<token>@github.com/username/repository.git
to clone from GitHub at the very least. I thought I would expand on how to do that, in case anyone comes across this answer like I did while trying to automate some cloning.GitHub has a very handy guide on how to do this, but it doesn't cover what to do if you want to include it all in one line for automation purposes. It warns that adding the token to the clone URL will store it in plaintext in
.git/config
. This is obviously a security risk for almost every use case, but since I plan on deleting the repo and revoking the token when I'm done, I don't care.1. Create a Token
GitHub has a whole guide here on how to get a token, but here's the TL;DR.
2. Clone the Repo
Same as the command @plosco gave,
git clone https://<token>@github.com/<username>/<repository>.git
, just replace<token>
,<username>
and<repository>
with whatever your info is.If you want to clone it to a specific folder, just insert the folder address at the end like so:
git clone https://<token>@github.com/<username>/<repository.git> <folder>
, where<folder>
is, you guessed it, the folder to clone it to! You can of course use.
,..
,~
, etc. here like you can elsewhere.3. Leave No Trace
Not all of this may be necessary, depending on how sensitive what you're doing is.
rm -rf <folder>
.git remote remove origin
or just remove the token by runninggit remote set-url origin https://github.com/<username>/<repository.git>
.Note that I'm no pro, so the above may not be secure in the sense that no trace would be left for any sort of forensic work.