0

I have .sh script that uses git for a pull request. While running it I always get requested for username and password. So how to make autofill for username?

pull.sh contents:

cd /home/test/project

git pull
Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58

3 Answers3

2

You could try using git-credential-cache. It will prompt for credentials just the first time.

To avoid username/password at alI I would suggest to use Ssh and Ssh keys for authentication (e.g: in gitlab).

Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58
  • 1
    If the repository is being accessed using the HTTP or HTTPS protocol (can be verified by running `git remote -v`), one can use the `~/.netrc` file to store the credentials (see [this](https://gist.github.com/technoweenie/1072829) or [this](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) for a formal manual page). The password is stored **in clear text** so _this file must have tight permissions set_ — such as `0600`. – kostix Oct 06 '18 at 17:24
  • Still, I personally find the Git credentials cache to be the best solution out there. – kostix Oct 06 '18 at 17:25
-1

Install expect tool in your system

Do following Stuff in your bash file

*#!/usr/bin/expect -f*

cd /home/test/project
git pull
expect "sername"
send your_username
send "\r"
expect "assword"
send {your_password}
send "\r"
interact

Hope it works !

-1

You can use expect script to interact with git to do git pull and then authentication.

#!/usr/bin/expect -f
spawn git pull
expect "username: " # write the keyword you get as prompt for entering username 
send "your_username\r"
# remove the below 2 lines if you do not want script to interact for password. 
expect "password: " # write the keyword you get as prompt for entering password 
send "your_password\r"
interact

if you don't want to store password, you can enter it as an argument. For example:

#!/usr/bin/expect -f
set my_password [lindex $argv 0] 
spawn git pull
expect "username: " # write the keyword you get as prompt for entering username 
send "your_username\r"
expect "password: " # write the keyword you get as prompt for entering password 
send "${my_password}\r"
interact

Run this script as ./myScript.sh <password> .

Rishabh Agarwal
  • 1,988
  • 1
  • 16
  • 33