python - gitpython 使用 PYTHON 检查文件是否有任何更改

标签 python git gitpython

我正在尝试 gitpython,而且我对它还很陌生。我正在尝试检测是否有任何需要提交的更改。

目前,我有一个如下所示的函数:

def commit(dir):
r = Repo(dir)
r.git.add(A=True)
r.git.commit(m='commit all')

但这只是提交目录的代码。我想做一些类似的事情,如果有更改,则显示一些消息,否则,显示另一条消息。

有人知道如何在 python 中做到这一点吗?

最佳答案

您可以像这样检查所有未暂存的更改:

for x in r.index.diff("HEAD"):
    # Just print
    print(x)

    # Or for each entry you can find out information about it, e.g.
    print(x.new_file)
    print(x.b_path)

基本上,您正在将暂存区域(即索引)与事件分支进行比较。

关于python - gitpython 使用 PYTHON 检查文件是否有任何更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42382487/

相关文章:

git - repo 历史上Git中枯死的 Twig 会永远存在吗?

python - GitPython 相当于 "git remote show origin"?

python - 没有名为 rest_authusers 的模块错误

sql-server - SQL Server 数据库与团队共享?

python - 将激活数据从电子邮件发送回 Django 服务器的代码

Git 产生 "BUG: There are unmerged index entries"

git - 相当于 git-archive 直接到磁盘,没有任何 tar 处理

python - 如何使用 gitpython 获取提交作者姓名和电子邮件?

python - 在 Django 中仅删除某些多对多关联

python - @csrf_exempt 在 Django 1.4 中停止工作