1

I have the following code in a Jupyter Notebook's cell:

!git push origin master

which will ask my password for Github but the cell keeps on running as I can't find the way to input my password. For some reason, I want to push the code this way.

I tried to follow the similar questions but nothing seems to be working in my case. Here's what I tried and it didn't work:

import getpass
import os

password = getpass.getpass()
command = "git -S push origin master " #can be any command but don't forget -S as it enables input from stdin
os.system('echo %s | %s' % (password, command))

Here's the log for above:

unknown option: -S
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]
Faizan Ali
  • 973
  • 2
  • 16
  • 32

1 Answers1

0

The -S is an option of sudo, not git... as you can see git is not even asking for password it just tells you that -S is invalid.

I believe git uses a secure way to get the password, reading from the tty and not stdin. It's probably quite hard to get a hand on the correct tty to input that password to make this work. Moreover this means that you have to write the password in plaintext in your notebook.

The correct way to handle this is to:

  • Generate an ssh key using ssh-keygen without a passphrase
  • Configure your server to use that key
  • Configure git to use the SSH protocol to do the push

This completely avoids a request for passwords.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
  • Okay, writing in plain text will work also but how can I do that? Passing the password as string to `echo %s | %s` that also doesn't work. I will try ssh though. – Faizan Ali Aug 11 '18 at 11:01
  • @FaizanAli I will not provide any information about that, since other people may think it is a good idea, which is not. One solution is to modify the repository configuration to use the `repo` HTTPS url with credentials in it. Again this avoids any password prompt but you have the credentials in plaintext in your repository configuration. Check [this question on ServerFault](https://serverfault.com/questions/815043/how-to-give-username-password-to-git-clone-in-a-script-but-not-store-credential) for some more information. Obviously you can change this configuration from the notebook... – Bakuriu Aug 11 '18 at 11:07
  • I found my solution using access tokens through that ServerFault question, thanks for that. – Faizan Ali Aug 11 '18 at 11:20