Jake
Jake

Reputation: 3471

Username and password in command for git push

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.

Upvotes: 157

Views: 358612

Answers (9)

Narek Tootikian
Narek Tootikian

Reputation: 1443

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.

Upvotes: 0

Mohammad Momtaz
Mohammad Momtaz

Reputation: 635

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

Upvotes: 14

ynux
ynux

Reputation: 1366

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

Upvotes: 0

Christopher Adams
Christopher Adams

Reputation: 1290

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

Upvotes: 29

Hamidreza
Hamidreza

Reputation: 855

I used below format

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

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

Upvotes: 47

VonC
VonC

Reputation: 1323223

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.

Upvotes: 3

poke
poke

Reputation: 387527

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).

Upvotes: 2

Alexey Orlenko
Alexey Orlenko

Reputation: 722

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.

Upvotes: 3

number5
number5

Reputation: 16423

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

Upvotes: 206

Related Questions