154

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

git clone https://username:password@myrepository.biz/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.

Jake
  • 3,411
  • 4
  • 21
  • 37

9 Answers9

203

Yes, you can do

git push https://username:password@myrepository.biz/file.git --all

in this case https://username:password@myrepository.biz/file.git replace the origin in git push origin --all

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

number5
  • 15,913
  • 3
  • 54
  • 51
  • 17
    Note that this will store username and password *in plaintext* in the git configuration. – poke Apr 21 '15 at 15:29
  • 5
    instead of plain password you can use password hash – Vasiliy vvscode Vanchuk Apr 21 '15 at 15:33
  • 2
    @VasiliyVanchuk What kind of hash do you use? Does this work for Github? – Raphi Jun 01 '16 at 19:58
  • 27
    what happen if password contain `@` ? – Rahul Sharma May 11 '17 at 07:20
  • 3
    Recommend enclosing the complete path with `' '`, for example: `git push 'https://username:password@myrepository.biz/file.git'` – AlikElzin-kilaka May 31 '18 at 20:18
  • Enclosing the complete path with ' ' will give you this error (Windows command line): fatal: I don't handle protocol ''https' – pasx Oct 03 '18 at 10:14
  • if you are using this command on windows - wait for few seconds - git bash will give prompt for username and password - and once you enter new username and password - it stores again – Ashish Shetkar Nov 30 '18 at 10:33
  • 1
    https://username@myrepository.biz/file.git => this to prevent storing password in plane text and thus every time you push it ask you to enter password – Abdullah Tahan Jul 10 '19 at 10:31
  • Thanks! Solved my problem. Note that the username and email will be saved in the `config` file of that git repo. – Desmond DAI Sep 23 '19 at 06:36
  • does not work on git bash on windows if username is email : fatal: unable to access 'https://gmail.com:password@git.heroku.com/silbeni.git/': URL using bad/illegal format or missing URL – Wardruna Jan 09 '21 at 14:36
  • @RahulSharma if the password contains @ then you can use %40 ( url encoder) – Singhak May 24 '21 at 04:57
  • URL using bad/illegal format or missing URL – Ravi Soni May 12 '22 at 14:15
  • This is useful if you wanna do git push in a github actions workflow without using external apps, using GITHUB_TOKEN as password. – Benur21 May 25 '22 at 15:39
  • Support for password authentication was removed on August 13, 2021. Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication. – Narek Tootikian Oct 13 '22 at 11:29
47

I used below format

git push https://username:password@myrepository.biz/file.git --all

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

Hamidreza
  • 807
  • 7
  • 9
  • You can even use WebUtility.UrlEncode(password) to get that %40 – pradeepradyumna Aug 19 '19 at 11:26
  • 1
    for php users use this `urlencode($password)` this will encode all the special characters not only `@`. – shyammakwana.me May 01 '20 at 10:28
  • Unsafe. If somebody intercepts and decrypts your sent message he gets your credentials... Use SSH key pairs... – 71GA Apr 14 '22 at 09:26
  • Support for password authentication was removed on August 13, 2021. Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication. – Narek Tootikian Oct 13 '22 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://YOUR_GIT_USERNAME@github.com/YOUR_GIT_USERNAME/yourGitFileName.git
Christopher Adams
  • 1,240
  • 10
  • 14
  • 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 '20 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? – TheTechRobo the Nerd Sep 09 '20 at 12:20
  • @TheTechRobo36414519 Apologies for my comment. It was caused by another permission issue. – Preet Sangha Sep 10 '20 at 22:23
  • It didn't prompt Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication. Support for password authentication was removed on August 13, 2021. – Narek Tootikian Oct 13 '22 at 11:30
  • 2023 still works like a charm! – Mike Victoria Apr 09 '23 at 15:23
9

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

Mohammad Momtaz
  • 545
  • 6
  • 12
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.

Alexey Orlenko
  • 682
  • 5
  • 8
  • 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 '15 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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
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 (git@host.com/repository.git) you use SSH keys and those you can remember using ssh-agent (or PuTTY’s pageant if you’re using plink).

poke
  • 369,085
  • 72
  • 557
  • 602
  • 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 '15 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 '15 at 15:28
  • 5
    It still doesn't answer his question, though. – Marvin Apr 21 '15 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

ynux
  • 1,280
  • 14
  • 21
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.

Narek Tootikian
  • 1,323
  • 2
  • 9
  • 15