How to configure automatic password handling for git commands:

2

I have an automatic build script for my build server that builds Android applications. As part of this building process, I need to pull the current sources from the repository to make a release and make a push command at the end of the build process to update the repository with the files that were changed during the build process by the build server.

So I have to build the following bash script:

#!/bin/bash
clear
echo "Start of Pull command"
git pull
echo "End of Pull command"
echo "Start of incrementedRelease build"
gradle incrementedRelease
echo "End of incrementedRelease build"
echo "Start of Commit command"
git commit -a -m "======================== Commit to change Manifest Version ======================"
echo "End of Commit command"
echo "Start of Push command"
git push
echo "End of Push command"

The issue is in the command of git push and git pull, git prompts me for a password.

Now I tried to run this command to create a password store, before entering the password for git.

  sudo git config --global credential.helper store

with and without the sudo command, but next time I run the script it's still prompts me for a password.

Could some one tell me how I can store the git password so I won't be prompted for each pull or push command? preferably take it from a file, and not use ssh, because I don't have access to the git server in order to generate the keys there..

UPDATE: This are the folder I have in the user's home folder:

enter image description here

Thanks in advance.

Emil Adz

Posted 2014-09-17T11:18:04.047

Reputation: 135

Answers

2

Setting the credential.helper config option to store should definitely do what you're asking for. You'll still have to enter your username and password at least once for git-credential-store to store it in a file (~/.git-credentials by default), but then you shouldn't need to anymore.

Note that the file storing your credentials won't be encrypted and anybody managing to read it would then get the user's permissions on the repository, so it might a good idea to set up a read-only user especially for this and not globally set the credential.helper config option to store.

If you're using a web interface to your repository (Github, Bitbucket, Gitlab, etc), you might also have the (preferred, imho) possibility to set up a SSH deploy key through the repository settings to provide secure, read-only access.

Likeyn

Posted 2014-09-17T11:18:04.047

Reputation: 131

Well yeah, I see from the docs that it should work, but for some reason it does not. And I can't find a good explanation why. – Emil Adz – 2014-09-23T10:07:50.257

1@EmilAdz How's your ~/.git-credentials file? Does it exist and have correct file permissions? Is there something in it? – Likeyn – 2014-09-23T12:03:00.563

For some reason I can't find this file, maybe I just don't understand where it should be located in linux. – Emil Adz – 2014-09-23T12:18:25.750

@EmilAdz It should be in the home folder of the user running the git commands, usually /home/username. Also note that files starting with a "." (dot) are hidden. – Likeyn – 2014-09-23T13:14:32.037

please see the the update of the folder I have under home/user. I think this folder is not existing for me. – Emil Adz – 2014-09-23T13:25:04.847

@EmilAdz Hmm there's no .git-credentials file and your local .gitconfig file seems to be owned by 'root' when I think it should belong to 'user'... Try running git config -l | grep credential. If your config option is properly set, it should appear in the result. If it doesn't, chown user:user the .gitconfig file, edit it to add your config option in it, and try using the credential store again. – Likeyn – 2014-09-23T15:11:29.440

Thanks for this informative comments, I'm an amateur in Linux and having struggle doing the simplest staff. I will try your tips and let you know if it's worked. – Emil Adz – 2014-09-23T15:38:31.370

When I run the grep command the result is: credential.helper=store so I guess the config file is configured properly. – Emil Adz – 2014-09-23T15:42:20.283

@EmilAdz Seems like it yeah, although it's kinda odd. Last thing I can think of: check the user owns and can actually write to its own home folder. – Likeyn – 2014-09-23T16:04:00.497

Seems like because the link the source server was an ssh link I had no other way but to use ssh keys to make the connection secure and without password prompts. – Emil Adz – 2014-10-02T15:54:25.767

0

Assuming you're on linux: This tutorial describes how to establish an ssh-connection to a remote server without typing your password.

If you follow these steps, git will not longer ask you for your credentials.

Furthermore, you will be able to ssh from commandline to your server without password.

user258346

Posted 2014-09-17T11:18:04.047

Reputation: 163

1This tutorial describes how to connect to my build server without any password. What I need is to make changes to git without any password. So this is not what I need, but thanks for you help – Emil Adz – 2014-09-17T12:38:29.413

I think, it is what you need, since git uses ssh as well: I'm using it every day: "git pull, git push, git remote update, etc" ... ;) – user258346 – 2014-09-17T13:10:01.367

I tried to follow this tutorial from the build server side, but for some reason when I try to connect to git using the "ssh hostname -l username" command I get this error: fatal: What do you think I am? A shell? Connection to my_host_name closed. – Emil Adz – 2014-09-17T14:18:51.890

What exactly do you mean with "try to connect to git"? The tutorial is expected to resolve the following issue: When typing "git push" in a shell on your local machine to push to your remote (build) server, you will not be asked anymore to enter your password. ... or do you try to connect from your build server to your local machine (which should in principle work as well. – user258346 – 2014-09-18T13:44:39.477

well, unfortunately I don't have access to the remote server, and hence I can't generate an ssh key there. So I need to find some kind of solutions without ssh but with user/password mechanism. – Emil Adz – 2014-09-18T14:04:10.993

Ok. I see your point. – user258346 – 2014-09-18T15:46:18.680