5

I have to write some scripts to automate "git remote" commands. But these commands prompt for password when invoked. How can I make it so the commands will not prompt for a password and the script will run without a prompt.

Chandu
  • 1,837
  • 7
  • 30
  • 51
  • One simple solution is to use a ssh key. – ckruczek Feb 10 '16 at 05:49
  • 1
    How about you go to your prefered search engine and try to find the solution by your own. If you have any question, AFTER you searched, don't hesitate to come back. – ckruczek Feb 10 '16 at 05:58

3 Answers3

2

Use the git-credential-store

If you use the Git helper store it will provide the needed authentication for a particular request pattern and will compare that pattern with the credentials file. It shouldn't prompt you for a username or password as long as the store has access to the credentials file. More information on this technique can be found in the Git Documentation:

This process will store your passwords unencrypted on disk, protected only by filesystem permissions.

$ git config credential.helper store

$ git push http://example.com/repo.git

Username: <type your username>

Password: <type your password>

There is a drawback to this technique as mentioned above, your credentials are not encrypted.

Community
  • 1
  • 1
Fergus
  • 2,821
  • 2
  • 27
  • 41
  • 1
    The OP explicitly said "in the command itself". Credential store is not "in the command itself". Your answer blatantly ignores the crucial constraint explicitly stated in the question. Downvoted. – Szczepan Hołyszewski Jan 08 '23 at 20:56
0

Storing password on scripts is not a good things.

Best way will be is to add SSH public key to git remote repository

1) in your home directory ssh-keygen -t rsa and copy ~/.ssh/id_rsa.pub to remote git repository

2) Now it will not ask for your password if you are using SSH type URL for git remote commands.

check it from .git/config file. In remote section see it has ssh type or https type URL

if it is https type then change it to ssh type by following command

git remote set-url origin git+ssh://git@gitlab.com/repository.git

Now it will not ask for your password.


If you planning to run this scripts on multiple system them generate public ssh keys for all and add them on remote repository.

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
0

Based on this answer, you can combine the credential.helper config setting with the ability to specify one-off configuration overrides with the -c option:

git -c credential.helper='!f() { sleep 1; echo "username=yourUserName"; echo "password=yourPassword"; }; f' your-git-action

It is a bit verbose, but it might just do it for you if you don't need to do it frequently. As opposed to all the other answers, this one strictly honors your requirements: the credentials are embedded in the command itself, and no permanent changes to git configuration or the environment are involved.

Bonus (as per the original answer): since the syntax of the credential.helper config value is simply shell, you can use shell variables in there, thus avoiding putting credentials into scripts.

Szczepan Hołyszewski
  • 2,707
  • 2
  • 25
  • 39