How to enter command with password for git pull?

Viewed 413338

I want to do this command in one line:

git pull && [my passphrase]

How to do it?

10 Answers

Below cmd will work if we dont have @ in password: git pull https://username:pass@word@mygithost.com/my/repository If you have @ in password then replace it by %40 as shown below: git pull https://username:pass%40word@mygithost.com/my/repository

Using the credentials helper command-line option:

git -c credential.helper='!f() { echo "password=mysecretpassword"; }; f' fetch origin

I just went through this so supplying my answer as I ended up using Gitlab access tokens although some may need Github access tokens

I would prefer to use SSH but there is a gitlab bug preventing that. Putting my password in .netrc or the URL is not ideal. Credential manager needs to be restarted on server reboot which is not ideal.

Hence I opted for an access token which can be used like so: git clone https://<username>:<accessToken>@gitlab.com/ownerName/projectName.git

The access token is stored with the url so this is not secure but it is more secure than using username:password since an access token can be restricted to certain operations and can be revoked easily.

Once it's cloned down (or the URL is manually updated) all your git requests will use the access token since it's stored in the URL.

These commands may be helpful if you wish to update a repo you've already cloned:

git remote show origin

git remote remove origin

git remote add origin https://<username>:<accessToken>@gitlab.com/ownerName/projectName.git

Edit: Be sure to check the comments below. I added 2 important notes.

You can just use username no need to use password since password will be prompted in git bash.

git pull https://username@github.com/username/myrepo

or

 git pull https://username@github.com/myrepo

I did not find the answer to my question after searching Google & stackoverflow for a while so I would like to share my solution here.

git config --global credential.helper "/bin/bash /git_creds.sh"
echo '#!/bin/bash' > /git_creds.sh
echo "sleep 1" >> /git_creds.sh
echo "echo username=$SERVICE_USER" >> /git_creds.sh
echo "echo password=$SERVICE_PASS" >> /git_creds.sh

# to test it
git clone https://my-scm-provider.com/project.git

I did it for Windows too. Full answer here

If you are looking to do this in a CI/CD script on Gitlab (gitlab-ci.yml). You could use

git pull $CI_REPOSITORY_URL

which will translate to something like:

git pull https://gitlab-ci-token:[MASKED]@gitlab.com/gitlab-examples/ci-debug-trace.gi

And I'm pretty sure the token it uses is a ephemeral/per job token - so the security hole with this method is greatly reduced.

Related