python - 如何使用 Python 检查 Git Repo 是否有未提交的更改

标签 python git

我对 git 很陌生,但我正在尝试使用 python 来检查 git 存储库是否有任何未提交的更改。无论我尝试使用 python 运行什么命令,我似乎都会遇到相同的错误。这是我的代码:

from git import *
repo = Repo("path\to\my\repo")
lastCommit = repo.head.commit.committed_date
uncommitted = repo.is_dirty()

一切都按预期工作,直到我运行最后一行,即出现错误:

Traceback (most recent call last):
.
.
.
    raise GitCommandNotFound: [Error 2] The system cannot find the file specified 

我也尝试过使用其他命令,但遇到了相同的错误。例如,repo.index.diff(repo.head.commit)。我还尝试运行 repo.index.diff(None)repo.index.diff('HEAD') ,它们给出了相同的错误。我真正想要的是为我命名为 repo 的存储库运行 $ git status 。我在 Windows 7 上使用 Python 2.7.9 和 gitpython 1.0.1。任何帮助将不胜感激!

最佳答案

在您的特定示例中(仅用于说明目的),您的 "path\to\my\repo" 将被理解为 'path\to\\my\repo' 。在路径的各个部分之间使用双反斜杠 ("path\\to\\my\\repo")。 \t 被理解为制表符,\r 被理解为回车符。或者,您可以在路径前面放置一个 r,如下所示:r"path\to\my\repo"

关于python - 如何使用 Python 检查 Git Repo 是否有未提交的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31540449/

相关文章:

Python - 时间单位转换的最佳实践

python Selenium : Can't find element by xpath when browser is headless

windows - Git (TortoiseGit) - 如何将单个文件恢复到以前的修订版,然后撤消恢复?

checkout 、 pull 、 checkout 、 merge/ rebase 的 Git 快捷方式

python - 混洗数据框中的列

python - 在 VS Code 中调试时如何提供输入数据?

python - 如何避免比较列表中的重复字符串

Git 说我删除了放入新文件夹中的照片?

Git 使用补丁

c# - 为什么从 C# 代码中获取 Git 不起作用