Piping credentails to git push

1

Whenever I execute git push, I have to type my username and password at the prompt manually.
However, I want to automate this process.

So, I tried to pass the credentials to the command using piping:

printf 'username\npassword' | git push

but the prompt for username and password still don't go away!
Why is it not working?

Note:
I know that this can be done using:
git push 'https://username:password@github.com/username/repo.git'
but I'm interested in knowing what's wrong with the piping method?


Also, to confirm that this process works for other cases, I did an experiment.
I created a script b.py:

b.py:

#!/usr/bin/env python3

username = input()
password = input()

log = open("log.txt", "w")
print(username + "\n" + password, file=log)

Then, I executed printf 'abcd\nefgh' | ./b.py in the terminal which did work as expected and resulted in the log file containing the username and password strings:

log.txt:

abcd
efgh

Anmol Singh Jaggi

Posted 2016-05-18T22:00:07.370

Reputation: 211

Answers

1

This would only work in the case of sequential prompts that are the immediate part of the script as per your example. However in the case of git your values are being passed to the process prior to any username / password prompt - probably while its doing everything (network, etc) before GIT_ASKPASS - For reference: https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables

aphorise

Posted 2016-05-18T22:00:07.370

Reputation: 136

So, is there any way to make it work? – Anmol Singh Jaggi – 2016-05-19T09:52:42.443

Not that I'm aware. Other than that the in-lined method you referenced where the credentials are part of the address. Also the other thing to bare in mind is git reliance on the transport used - since it does not manage that - so I'd imagine that there would also be separation there so piping would probably not be possible without resorting to additional hacks. – aphorise – 2016-05-19T11:13:36.243