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 kn
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/
git config --global core.askpass
Run this first before cloning the same way, should be fixed!
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.
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.
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.
GitHub has a whole guide here on how to get a token, but here's the TL;DR.
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.
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 running git 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.
On Windows, the following steps should re-trigger the GitHub login window when git clone
ing: