1276

I know how to include a username and password in a https Git URL like this:

git clone https://username:password@host

But I'd like to know how to provide a username and password to an SSH remote like this:

git clone git@host.git

I've tried like this:

git clone username:password@git@host.git
git clone git@username:password@host.git
git clone git@host.git@username:password

But they haven't worked.

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
coordinate
  • 15,224
  • 6
  • 24
  • 24
  • 5
    You can't. The "git" before the "@" is already a username. From where did you get the repository URL (git@remote.get)? From where did you get the idea that you have to provide a different username and a password? – Ken Thomases Apr 07 '12 at 12:54
  • 3
    My repos URL is from heroku "git@heroku.com:zxy-helloworld.git". And I use emacs shell to clone and push. If the shell asks for password, emacs will hang. This is a known issue with emacs on Windows: https://www.gnu.org/software/emacs/windows/Sub_002dprocesses.html#index-subprocesses_002c-hanging-when-reading-input-213 – coordinate Apr 08 '12 at 00:50
  • 4
    What if the username is an email? how do you type it? https://myemail@gmail.com@example.com ? – Kyon Perez Apr 15 '20 at 03:27
  • See [GitHub's instruction on _how_ to create a token and that why it's _better_ due to scoping and permissions](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) – mfaani Jun 05 '20 at 18:07
  • @Honey the question is not GitHub specific. – Szczepan Hołyszewski Jul 03 '20 at 13:44

16 Answers16

1888

Based on Michael Scharf's comment:

You can leave out the password so that it won't be logged in your Bash history file:

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

It will prompt you for your password.

Alternatively, you may use:

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

This way worked for me from a GitHub repository.

vhs
  • 9,316
  • 3
  • 66
  • 70
Bassetassen
  • 20,852
  • 8
  • 39
  • 40
  • 7
    it doesn't have to be the same by the way. If your colleague created the git repo and you are logging in as another account, they will not be the same. – holgac Sep 27 '13 at 17:21
  • 74
    It is not advisable to put the password in the URL for this file is saved on the .git/config . Is not safe, it is better to use ssh key – zetanova Feb 10 '14 at 18:48
  • 1
    @Zephiro your advice is poor, unless you also tell people to use a strong passphrase on their private key. Otherwise, there is not much difference in having a cleartext password vs an unprotected private key on your hard drive. – fabspro Mar 04 '14 at 02:23
  • 62
    @MrDuk no problem, escape it (as %40 iirc). – Benjamin Gruenbaum Apr 06 '14 at 14:52
  • @Bassetassen Works fine for clone. Does not work for pull. http://stackoverflow.com/questions/11506124/how-to-enter-command-with-password-for-git-pull – RuntimeException Aug 29 '14 at 15:19
  • 74
    You can leave out the password: `git clone https://username@github.com/username/repository.git`. git will ask you and it will not be saved in .git/config nor in your bash history – Michael_Scharf Feb 26 '15 at 00:52
  • 55
    you can also type 'space' at the beginning of the line so that linux won't store the command in history – Nicolas Zozol Feb 17 '16 at 08:36
  • this is especially useful for old `git` versions, who do not ask for both "username" and "password" when cloning a repo on a fresh machine – Liviu Chircu May 24 '16 at 10:43
  • 6
    You can also add a space in front of the command. This way, it won't be stored in your bash history. – fedorqui Dec 15 '16 at 15:06
  • 1
    Not so easy if the password contains unusual characters. – matt Apr 04 '17 at 16:09
  • 1
    second username was creating issues removing that worked for me: ```git clone https://username:password@github.com/repository.git``` – jayesh hathila Apr 19 '17 at 11:16
  • 3
    How to use if my password contains '@' symbol – Atul Agrawal May 31 '17 at 10:32
  • @AtulAgrawal See Benjamin Gruenbaum's comment – Bassetassen May 31 '17 at 18:46
  • 13
    Another option I like is using and auth token instead of the username to get past any prompts `git clone https://@github.com/username/repository.git` – plosco Jun 14 '17 at 01:19
  • I had a situation where I was already logged on as another user. I tried the second option of this answer, and it said "Authentication failed." I merely tried it a second time, and it prompted me for the password and worked. – Marvo Dec 16 '17 at 00:00
  • when I use this command on google colab, this error is received: fatal: could not read Password for 'https://username@github.com': No such device or address, and it does not ask me for password – Atena Jan 14 '19 at 13:50
  • 1
    Thanks a ton! Works well for PULL on bitbucket.org as well : git pull https://[user]:[password]@bitbucket.org/[bitbucket-account]/[project].git [branch] – anup Jan 31 '19 at 16:22
  • 1
    I had many different special characters in the password, the percent encoding helped me to include the password as part of URL. Reference: https://fabianlee.org/2016/09/07/git-calling-git-clone-using-password-with-special-character/ – Santosh Kumar Arjunan Mar 05 '19 at 06:56
  • 2
    if you use the format ``git clone https://username:password@github.com/username/repository.git`` and the error message ``URL using bad / illegal format or missing URL`` appears, then this is probably a special character in your password, then use ``git clone https://username@github.com/username/repository.git`` to wait for prompt. – Sma Ma May 10 '19 at 08:00
  • Why are you even mentioning the first unsecure method and why do you put it in the first place? – Black Sep 04 '19 at 06:14
  • @Black, what is exactly unsecured? URLs are encrypted so username and password are secured. – Maksim Shamihulau Nov 11 '19 at 16:15
  • @MaksimShamihulau, it will show in your "history" file with your password in clear text – Black Nov 12 '19 at 08:55
  • 1
    `password` would be replaced with `private_token` with GitLab. – licaomeng Feb 04 '20 at 17:48
  • my password has @ as a special character , how do i deal with it? – srikar kulkarni May 01 '20 at 12:09
  • For Docker users, if you need to use HTTPS during the build phase to pull your project, you can clear the bash history just after with the `history -c` command. – toshiro92 May 21 '20 at 08:44
  • THE QUESTION IS NOT GITHUB SPECIFIC, and neither should be the answer. – Szczepan Hołyszewski Jul 03 '20 at 13:47
  • 2
    This is now deprecated and Github recommends using Personal Tokens or SSH over this. https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ – karan_for_you Dec 21 '20 at 10:18
  • This solution worked, I couldn't clone a github repo because my git configs had other credentials. cmd and SourceTree didn't work but using this command I was able to clone successfully. – Cortex Mar 05 '21 at 18:16
  • 1
    Why, if the question is about SSH, you answer with an HTTPS solution? Is the solution possible for SSH? No? Then write it in your answer and, if you want, tell wahy. – Matteo Gaggiano Jun 15 '21 at 07:42
  • Hello. How do i insert a password that containts @ ? – Raul Chiarella Dec 17 '21 at 20:33
  • This should not be done on a shared server as the password will be stored in the git remote URL. – doublespaces Aug 26 '22 at 18:26
  • thanks bro worked for me, just needed to replace password with of github – Akash Bhardwaj Apr 01 '23 at 18:49
  • Replace the @ symbol in your password with %40. Url encode any other special characters. Try https://www.urlencoder.io/ but I'd recommend only putting in the special character(s) and not your whole password. ;) – Rich C Aug 24 '23 at 20:18
278

The user@host:path/to/repo format tells Git to use ssh to log in to host with username user. From git help clone:

An alternative scp-like syntax may also be used with the ssh protocol:

[user@]host.xz:path/to/repo.git/

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 time

If 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 into ssh-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 and bar. Your ssh key for foo is ~/.ssh/foo_github_id and your ssh key for bar is ~/.ssh/bar_github_id. You want to access git@github.com:foo/foo.git with your foo account and git@github.com:bar/bar.git with your bar account. You would add the following to your ~/.ssh/config:

Host gh-foo
    Hostname github.com
    User git
    IdentityFile ~/.ssh/foo_github_id
Host gh-bar
    Hostname github.com
    User git
    IdentityFile ~/.ssh/bar_github_id

You would then clone the two repositories as follows:

git clone gh-foo:foo/foo.git  # logs in with account foo
git clone gh-bar:bar/bar.git  # logs in with account bar

Avoiding ssh altogether

Some services provide HTTP access as an alternative to ssh:

  • GitHub:

    https://username:password@github.com/username/repository.git
    
  • Gitorious:

    https://username:password@gitorious.org/project/repository.git
    
  • 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:

git config --global credential.helper cache
git config --global credential.https://github.com.username foo
git clone https://github.com/foo/repository.git

The above will cause Git to ask for your password once every 15 minutes (by default). See git help credentials for details.

Community
  • 1
  • 1
Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
  • 1
    This is the best explanation of how git works with SSH. My understanding is that when you specify git@githost.com:path/to/repo.git, it effectively tells git that the user is git itself and that it should go grab your credentials (public key) from the ssh-agent for the Host "githost.com". – Anael Dec 12 '19 at 03:52
  • Brilliant, I've only one github but used since I have 2 emails for different projects in other repos, replacing git@github.com with the custom name which locates the relevant IdentityFile for the particular email works perfectly! – boardtc Oct 26 '22 at 12:44
133

Follow this arguments to replace with ur special cases if they are creating an issue:

 !   #   $   &   '   (   )   *   +   ,   /   :   ;   =   ?   @   [   ]

%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D

So for example-

actual URL: https://usern@me:p@ssword@git/reponame.git

Solution URL to use: https://usern%40me:p%40ssword@git/reponame.git

WurmD
  • 1,231
  • 5
  • 21
  • 42
Anshuman Bardhan
  • 1,986
  • 2
  • 8
  • 10
88

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.

  1. Go to Settings > Developer Settings > Personal Access Tokens (here's a direct link)
  2. Click "Generate a New Token" and enter your password again. (here's another direct link)
  3. Set a description/name for it, check the "repo" permission and hit the "Generate token" button at the bottom of the page.
  4. Copy your new token before you leave the page

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.

  • You probably don't want to leave that token hanging around if you have no intentions of using it for some time, so go back to the tokens page and hit the delete button next to it.
  • If you don't need the repo again, delete it rm -rf <folder>.
  • If do need the repo again, but don't need to automate it again, you can remove the remote by doing git remote remove origin or just remove the token by running git remote set-url origin https://github.com/<username>/<repository.git>.
  • Clear your bash history to make sure the token doesn't stay logged there. There are many ways to do this, see this question and this question. However, it may be easier to just prepend all the above commands with a space in order to prevent them being stored to begin with.

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.

Community
  • 1
  • 1
Rob Rose
  • 1,806
  • 22
  • 41
  • 5
    `https://@github.com` was not accepted with my portal, I had to use `https://oauth2:@github.com` – MushyPeas Jun 27 '19 at 10:19
  • @MushyPeas This answer using just `` worked fine for me using a Personal Access Token. – Taylor D. Edmiston Feb 09 '22 at 18:26
  • > _However, it may be easier to just prepend all the above commands with a space in order to prevent them being stored to begin with._ < That is not foolproof! Depending on the settings of `HISTCONTROL` even commands beginning with a space might be stored in the history. See also [this SO answer](https://unix.stackexchange.com/a/483705/409795). – ToJo Apr 22 '22 at 09:57
44

Post 13 Aug 2021 you must use

  • Git Clone

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

To generate token:

Settings >> Developer settings >> personal access tokens >> generate new token

  • Git Push

After successful cloning, Next time when you do git push you won't have to again mention the username. you can confirm this by opening the .git/config file in notepad it will show

[remote "origin"]
    url = https://username:tokenxxxxxxxxxx@github.com/username/repo.git
Harshal Deore
  • 1,050
  • 1
  • 11
  • 11
36

Though there are many answers, myself facing the repeated issue when username or password has special characters in it.

URL encode your username and password for git, then use it as part of URL itself (when there is no security concern).

Say, URL encoded value of username

'user+1' is user%2B1

and URL encoded value of password

'Welcome@1234' is Welcome%401234

Then your GIT Clone URL would look like,

git clone https://user%2B1:Welcome%401234@actual-git-url-for-the-repo works perfectly, whereas,

git clone https://user+1:Welcome@1234@actual-git-url-for-the-repo gives you 403 errors

Hope this helps.

Just in case, want to URL encode online: https://www.urlencoder.org/

Santosh Kumar Arjunan
  • 3,600
  • 3
  • 23
  • 24
22

I solved this problem in the following way:

enter image description here

BehrouzMoslem
  • 9,053
  • 3
  • 27
  • 34
  • 19
    never a good idea to type your password in command line. Did you know your system may store command line history in a file? (Ex: Linux has an hidden file named .bash_history) – Augusto Feb 06 '19 at 20:49
  • Correct. Whenever I do this, or something like this, which at times can be very convenient, I always delete the command I used from the history (history -d). – Francesco Marchetti-Stasi Dec 19 '19 at 08:36
  • 12
    @FrancescoMarchetti-Stasi you don't need to delete the command, just use a space at the beginning of the command and it will never get stored in the history. – Ishtiyaq Husain Apr 21 '20 at 15:23
  • Yes--I learnt it only a couple of months ago, after almost 30 years on Unix systems... shame on me :) – Francesco Marchetti-Stasi Apr 21 '20 at 16:15
  • ...unless you are on a corporate server which stores commands you type even so. Passwords on the command line are never a good idea. – sastorsl Apr 19 '21 at 10:55
  • But the password will be still there in .git/config file. So it is not at all secure. – Swapnil Gangrade Jul 08 '21 at 19:34
20

To clone a repository, first you should generate an access token (you can't use your login password anymore) and then clone the repository with this generated token.

Generate access token:

github:
Settings > Developer settings > personal access tokens > generate new token

bitbucket:
Personal settings > App passwords > create app password

Clone the repository:

github:

$ git clone https://YOUR-USERNAME:GENERATED-TOKEN@github.com/YOUR-USERNAME/YOUR-REPOSITORY

bitbucket:

git clone https://YOUR-USERNAME@bitbucket.org/YOUR-REPOSITORY

It will prompt you for generated token.

Masoud Gheisari
  • 949
  • 1
  • 9
  • 21
16

On Windows, the following steps should re-trigger the GitHub login window when git cloneing:

  • Search start menu for "Credential Manager"
  • Select "Windows Credentials"
  • Delete any credentials related to Git or GitHub

result

Ore4444
  • 9,119
  • 2
  • 23
  • 29
13

If you're using http/https and you're looking to FULLY AUTOMATE the process without requiring any user input or any user prompt at all (for example: inside a CI/CD pipeline), you may use the following approach leveraging git credential.helper

GIT_CREDS_PATH="/my/random/path/to/a/git/creds/file"
# Or you may choose to not specify GIT_CREDS_PATH at all.
# See https://git-scm.com/docs/git-credential-store#FILES for the defaults used

git config --global credential.helper "store --file ${GIT_CREDS_PATH}"
echo "https://alice:${ALICE_GITHUB_PASSWORD}@github.com" > ${GIT_CREDS_PATH}

where you may choose to set the ALICE_GITHUB_PASSWORD environment variable from a previous shell command or from your pipeline config etc.

Remember that "store" based git-credential-helper stores passwords & values in plain-text. So make sure your token/password has very limited permissions.


Now simply use https://alice@github.com/my_repo.git wherever your automated system needs to fetch the repo - it will use the credentials for alice in github.com as store by git-credential-helper.

Rakib
  • 12,376
  • 16
  • 77
  • 113
12

I prefer to use GIT_ASKPASS environment for providing HTTPS credentials to git.
Provided that login and password are exported in USR and PSW variables, the following script does not leave traces of password in history and disk + it is not vulnerable to special characters in the password:

GIT_ASKPASS=$(mktemp) && chmod a+rx $GIT_ASKPASS && export GIT_ASKPASS

cat > $GIT_ASKPASS <<'EOF'
#!/bin/sh
exec echo "$PSW"
EOF

git clone https://${USR}@example.com/repo.git

NB: Note single quotes around heredoc marker 'EOF' which means that temporary script holds literally $PSW characters, not the password / expanded value of PSW variable

maoizm
  • 682
  • 13
  • 17
  • if you escape dollar sign - `\$PSW` you'll avoid unnecessarily embedding password into temporary file – Marcin Wisnicki Jun 10 '21 at 18:34
  • @Marcin that's exactly what my recipe is avoiding: the temporary file referred in GIT_ASKPASS holds just the name of PSW variable, not its value – maoizm Jun 10 '21 at 20:11
  • 1
    @MarcinWisnicki Please tell the name and version of your shell. It works perfectly in Ubuntu Dash (/bin/sh) 0.5.11 and Bash 5.1.8 (but is expected to work in earlier versions of both as well). Please check if you put single quotes around `EOF` in `cat >$GIT_ASKPASS <<'EOF'` as provided in the listing – maoizm Jun 14 '21 at 21:58
  • 1
    thanks, the qoutes around EOF made all the difference! – Marcin Wisnicki Jun 21 '21 at 22:22
6
git config --global core.askpass

Run this first before cloning the same way, should be fixed!

Rohan
  • 3,296
  • 2
  • 32
  • 35
4

This is an excellent Stack Overflow question, and the answers have been very instructive, such that I was able to resolve an annoying problem I ran into recently.

The organization that I work for uses Atlassian's BitBucket product (not Github), essentially their version of GitHub so that repositories can be secured completely on premise. I was running into a similar problem as @coordinate, in that my password was required for a new repository I checked out. My credentials had been saved globally for all BitBucket projects, so I'm not sure what prompted the loss of credentials.

In short, I was able to enter the following GIT command (supplying only my username), which then prompted Git's Credential Manager to prompt me for the password, which I was then able to save.

git clone https://user@code.domain.org/git/[organization]/[team]/[repository.git]

NOTE: the bracketed directory sub-paths simply refer to internal references, and will vary for you!

Michael M
  • 1,303
  • 1
  • 12
  • 12
0

In case you have the credentials stored in any kind of credential-helper, but not configured the credential helper to be active globally, you can clone like this:

git clone https://git.example.com/ns/repo.git --config=credential.helper=store

This sets the credential-helper config locally (for the new repo only) before cloning.

Torge Rosendahl
  • 482
  • 6
  • 17
0

HTTPS Git URLs

git clone https://user:password@host

Including the password in the Git URL is considered bad practice because it risks inadvertent credentials exposure in config files and command history. It also breaks if the password changes.

More secure and more reliable is to use a credential-generating helper such as Git Credential Manager (included in Git for Windows) or git-credential-oauth (included in several Linux distributions).

The first time you authenticate, the helper opens a browser window to the host. Subsequent authentication within storage lifetime is non interactive.

SSH Git URLs

SSH authentication requires an SSH key. This file is thousands of bytes; you can't include it in the URL. You can include the username in the URL:

git clone ssh://user@host

If the SSH key is protected by a passphrase, you can use ssh-agent to remember it.

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
0

Most of the time this works well.

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

However, There is another scenario when someone invites you to their organization's repo. and Let's say this is how the repo URL will look like.

https://github.com/organization/repo.git

Now in this scenario, the following URL

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

will not work. You need to make some modifications to it in the following way.

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

Hope this will help someone.

sohail amar
  • 381
  • 2
  • 14