gitpython-使用消息推送存储

时间:2019-04-15 15:32:21

标签: python git gitpython

我是gitpython的新手,并希望使用它来创建包含特定消息的存储。我知道它的命令行语法是git stash push -m "descriptive message here",但是我无法从gitpython中获得相同的命令来工作。根据{{​​3}}上的文档,似乎下面的代码应该可行

import git

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

但是它失败并显示以下错误

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'

如果我从错误消息中复制cmdline文本并粘贴到终端中,它将按预期工作

1 个答案:

答案 0 :(得分:1)

尝试repo.git.stash('push', '-m', 'descriptive message here')