How to enter command with password for git pull?

GitBashCommand

Git Problem Overview


I want to do this command in one line:

git pull && [my passphrase]

How to do it?

Git Solutions


Solution 1 - Git

This is not exactly what you asked for, but for http(s):

  • you can put the password in .netrc file (_netrc on windows). From there it would be picked up automatically. It would go to your home folder with 600 permissions.
  • you could also just clone the repo with https://user:pass@domain/repo but that's not really recommended as it would show your user/pass in a lot of places...
  • a new option is to use the credential helper. Note that credentials would be stored in clear text in your local config using standard credential helper. credential-helper with wincred can be also used on windows.

Usage examples for credential helper

  • git config credential.helper store - stores the credentials indefinitely.
  • git config credential.helper 'cache --timeout=3600'- stores for 60 minutes

For ssh-based access, you'd use ssh agent that will provide the ssh key when needed. This would require generating keys on your computer, storing the public key on the remote server and adding the private key to relevant keystore.

Solution 2 - Git

I found one way to supply credentials for a https connection on the command line. You just need to specify the complete URL to git pull and include the credentials there:

git pull https://username:[email protected]/my/repository

You do not need to have the repository cloned with the credentials before, this means your credentials don't end up in .git/config. (But make sure your shell doesn't betray you and stores the command line in a history file.)

Solution 3 - Git

Doesn't answer the question directly, but I found this question when searching for a way to, basically, not re-enter the password every single time I pull on a remote server.

Well, git allows you to cache your credentials for a finite amount of time. It's customizable in git config and this page explains it very well:

https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux

In a terminal, run:

$ git config --global credential.helper cache
# Set git to use the credential memory cache

To customize the cache timeout, you can do:

$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)

Your credentials will then be stored in-memory for the requested amount of time.

Solution 4 - Git

Below cmd will work if we dont have @ in password: git pull https://username:pass@[email protected]/my/repository If you have @ in password then replace it by %40 as shown below: git pull https://username:pass%[email protected]/my/repository

Solution 5 - Git

Using the credentials helper command-line option:

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

Solution 6 - Git

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.

Solution 7 - Git

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

git pull https://[email protected]/username/myrepo

or

 git pull https://[email protected]/myrepo

Solution 8 - Git

Note that the way the git credential helper "store" will store the unencrypted passwords changes with Git 2.5+ (Q2 2014).
See commit 17c7f4d by Junio C Hamano (gitster)

> ## credential-xdg

> Tweak the sample "store" backend of the credential helper to honor XDG configuration file locations when specified.

The doc now say:

> If not specified:

> - credentials will be searched for from ~/.git-credentials and $XDG_CONFIG_HOME/git/credentials, and

  • credentials will be written to ~/.git-credentials if it exists, or $XDG_CONFIG_HOME/git/credentials if it exists and the former does not.

Solution 9 - Git

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

Solution 10 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnton MedvedevView Question on Stackoverflow
Solution 1 - GiteisView Answer on Stackoverflow
Solution 2 - GitholgeroView Answer on Stackoverflow
Solution 3 - GitJivanView Answer on Stackoverflow
Solution 4 - GitSagar DeshmukhView Answer on Stackoverflow
Solution 5 - GitJetherView Answer on Stackoverflow
Solution 6 - GitBixView Answer on Stackoverflow
Solution 7 - GitKrishnamoorthy AcharyaView Answer on Stackoverflow
Solution 8 - GitVonCView Answer on Stackoverflow
Solution 9 - GitvdsbenoitView Answer on Stackoverflow
Solution 10 - GitMountainAshView Answer on Stackoverflow