Skip to content
Advertisement

How to input password to a shell command ‘git push’ from a Jupyter Notebook?

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>]

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement