155

It's possible to clone down a git repository, specifying username and password in the command. Example:

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

Is it is possible to also specify the username and password when pushing? So that, for example, running git push origin --all will then output asking for a password. I want this in one command.

(I am aware of the ability to set up keys and other solutions, but I want to know if there is a way to just keep using username and password with one command.) I am running Git Bash on Windows 8.1.

1
  • Do you find solution for git commit?
    – Nam Vu
    Oct 3, 2017 at 2:43

9 Answers 9

204

Yes, you can do

git push https://username:[email protected]/file.git --all

in this case https://username:[email protected]/file.git replace the origin in git push origin --all

To see more options for git push, try git help push

14
  • 17
    Note that this will store username and password in plaintext in the git configuration.
    – poke
    Apr 21, 2015 at 15:29
  • 5
    instead of plain password you can use password hash Apr 21, 2015 at 15:33
  • 2
    @VasiliyVanchuk What kind of hash do you use? Does this work for Github?
    – Raphi
    Jun 1, 2016 at 19:58
  • 31
    what happen if password contain @ ? May 11, 2017 at 7:20
  • 4
    Recommend enclosing the complete path with ' ', for example: git push 'https://username:[email protected]/file.git' May 31, 2018 at 20:18
47

I used below format

git push https://username:[email protected]/file.git --all

and if your password or username contain @ replace it with %40

4
  • You can even use WebUtility.UrlEncode(password) to get that %40 Aug 19, 2019 at 11:26
  • 1
    for php users use this urlencode($password) this will encode all the special characters not only @. May 1, 2020 at 10:28
  • Unsafe. If somebody intercepts and decrypts your sent message he gets your credentials... Use SSH key pairs...
    – 71GA
    Apr 14, 2022 at 9:26
  • Support for password authentication was removed on August 13, 2021. Please see docs.github.com/en/get-started/getting-started-with-git/… for information on currently recommended modes of authentication. Oct 13, 2022 at 11:30
29

For anyone having issues with passwords with special chars just omit the password and it will prompt you for it:

git push https://[email protected]/YOUR_GIT_USERNAME/yourGitFileName.git
5
  • 3
    It seems as if your answer is the only one on the entire web stating straight how to only give the username.
    – shuhalo
    Aug 21, 2020 at 14:13
  • @PreetSangha did you change YOUR_GIT_USERNAME to your usermame, and if you don't use github.com did you change it to something else? Did you change yourGitFileName.git to something else? Sep 9, 2020 at 12:20
  • @TheTechRobo36414519 Apologies for my comment. It was caused by another permission issue. Sep 10, 2020 at 22:23
  • It didn't prompt Please see docs.github.com/en/get-started/getting-started-with-git/… for information on currently recommended modes of authentication. Support for password authentication was removed on August 13, 2021. Oct 13, 2022 at 11:30
  • 2023 still works like a charm! Apr 9, 2023 at 15:23
13

Support for password authentication was removed on August 13, 2021. you can use a personal access token instead. like following push command:

git push https://<User Name>:<Token>@github.com/<User Name>/<Your Repository>.git

Link to how to create an access token

2
3

According to the Git documentation, the last argument of the git push command can be the repository that you want to push to:

    git push [--all | --mirror | --tags] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
             [--repo=<repository>] [-f | --force] [--prune] [-v | --verbose] [-u | --set-upstream]
             [<repository> [<refspec>…]]

And the repository parameter can be either a URL or a remote name.

So you can specify username and password the same way as you do in your example of clone command.

1
  • 2
    That seems suitable, but when I add the git repo with username and password as an argument, as the example, it still prompts for the password.
    – Jake
    Apr 21, 2015 at 15:33
3

It is possible but, before git 2.9.3 (august 2016), a git push would print the full url used when pushing back to the cloned repo.
That would include your username and password!

But no more: See commit 68f3c07 (20 Jul 2016), and commit 882d49c (14 Jul 2016) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 71076e1, 08 Aug 2016)

push: anonymize URL in status output

Commit 47abd85 (fetch: Strip usernames from url's before storing them, 2009-04-17, Git 1.6.4) taught fetch to anonymize URLs.
The primary purpose there was to avoid sticking passwords in merge-commit messages, but as a side effect, we also avoid printing them to stderr.

The push side does not have the merge-commit problem, but it probably should avoid printing them to stderr. We can reuse the same anonymizing function.

Note that for this to come up, the credentials would have to appear either on the command line or in a git config file, neither of which is particularly secure.
So people should be switching to using credential helpers instead, which makes this problem go away.

But that's no excuse not to improve the situation for people who for whatever reason end up using credentials embedded in the URL.

2

Git will not store the password when you use URLs like that. Instead, it will just store the username, so it only needs to prompt you for the password the next time. As explained in the manual, to store the password, you should use an external credential helper. For Windows, you can use the Windows Credential Store for Git. This helper is also included by default in GitHub for Windows.

When using it, your password will automatically be remembered, so you only need to enter it once. So when you clone, you will be asked for your password, and then every further communication with the remote will not prompt you for your password again. Instead, the credential helper will provide Git with the authentication.

This of course only works for authentication via https; for ssh access ([email protected]/repository.git) you use SSH keys and those you can remember using ssh-agent (or PuTTY’s pageant if you’re using plink).

3
  • 4
    "I am aware of the ability to set up keys and other solutions, but I want to know if there is a way to just keep using username and password with one command."
    – Marvin
    Apr 21, 2015 at 15:27
  • @Marvin I’m aware of that remark, however I didn’t just want to submit a two-sentence answer, so I included information for other users who might not be just interested in connecting via HTTPS.
    – poke
    Apr 21, 2015 at 15:28
  • 5
    It still doesn't answer his question, though.
    – Marvin
    Apr 21, 2015 at 15:29
0

6.5 years later, multi factor authorization is almost everywhere, and you'd probably use a token. For github, see Creating a personal access token

0

I solved this issue with GitCredentialManager

It is a terminal utility for git. When running git push, it will open a prompt to provide credentials. Please find more details on their GitHub Repo. https://github.com/GitCredentialManager/git-credential-manager

It is easy to install and use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.