Search code examples
pythongitgitpython

gitpython - push stash with message


I am new to gitpython and want to use it to create a stash with a specific message. I know the command line syntax for this is git stash push -m "descriptive message here", but I cannot get the same command to work from gitpython. Per the documentation on unwrapped commands, it seems like the code below should be possible

import git

repo = git.Repo('/path/to/my/repo')
repo.git.stash('push -m "descriptive message here")

However it fails with the following error

Traceback (most recent call last):
  File "/home/addison/miniconda3/envs/openalpr/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-28-827e00236a8a>", line 1, in <module>
    repo.git.stash('push -m "descriptive message here"')
  File "/home/addison/miniconda3/envs/openalpr/lib/python3.6/site-packages/git/cmd.py", line 548, in <lambda>
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
  File "/home/addison/miniconda3/envs/openalpr/lib/python3.6/site-packages/git/cmd.py", line 1014, in _call_process
    return self.execute(call, **exec_kwargs)
  File "/home/addison/miniconda3/envs/openalpr/lib/python3.6/site-packages/git/cmd.py", line 825, in execute
    raise GitCommandError(command, status, stderr_value, stdout_value)
git.exc.GitCommandError: Cmd('git') failed due to: exit code(1)
  cmdline: git stash push -m "descriptive message here"
  stderr: 'usage: git stash list [<options>]
   or: git stash show [<stash>]
   or: git stash drop [-q|--quiet] [<stash>]
   or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
   or: git stash branch <branchname> [<stash>]
   or: git stash save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
              [-u|--include-untracked] [-a|--all] [<message>]
   or: git stash [push [--patch] [-k|--[no-]keep-index] [-q|--quiet]
               [-u|--include-untracked] [-a|--all] [-m <message>]
               [-- <pathspec>...]]
   or: git stash clear'

If I copy the cmdline text from the error message and paste into terminal, it works as expected


Solution

  • Try repo.git.stash('push', '-m', 'descriptive message here').