如何使用GitPython获取暂存文件?

2024-09-28 21:50:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我用GitPython计算git中的暂存文件。在

对于修改过的文件,我可以使用

repo = git.Repo()
modified_files = len(repo.index.diff(None))

但对于暂存文件,我找不到解决方案。在

我知道git status --porcelain,但我正在寻找其他更好的解决方案。(我希望使用gitpython而不是git命令,脚本会更快)


Tags: 文件gitnoneindexlenstatusdiffrepo
1条回答
网友
1楼 · 发布于 2024-09-28 21:50:48

您已接近,请使用repo.index.diff("HEAD")在临时区域中获取文件。在


完整演示:

首先创建测试回购:

$ cd test
$ mkdir repo && cd repo && touch a b c && git init && git add . && git commit -m "init"
$ echo "a" > a && echo "b" > b && echo "c" > c && git add a b
$ git status
On branch master
Changes to be committed:
        modified:   a
        modified:   b
Changes not staged for commit:
        modified:   c

现在请登录ipython:

^{pr2}$

相关问题 更多 >