如何使用python检查git repo中是否存在未分页/未提交的更改或未推送的提交

2024-09-14 11:30:58 发布

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

如何使用python检查git repo中是否存在未老化/未提交的更改或未推送的提交

我只知道命令行

git status会告诉你的

  • 未时效变化
  • 未承诺阶段
  • 以及当前分支是否在远程服务器后面提交

如果我为函数提供根路径,例如(“C:/ProgramFiles”), 如果代码可以给出一个列表,其中每个元素都是

(path of this repos found under the root path, Unstaged/uncommited changes, Untracked files: , Latest commit is pushed)


Tags: path函数代码命令行git路径服务器远程
2条回答

您可以使用GitPython包提供帮助

pip install GitPython

然后,此脚本可以为您提供一个起点:

from git import Repo

repo = Repo('.')

print(f"Unstaged/uncommited changes: {repo.is_dirty()}")
print(f"Untracked files: {len(repo.untracked_files)}")

remote = repo.remote('origin')
remote.fetch()
latest_remote_commit = remote.refs[repo.active_branch.name].commit
latest_local_commit = repo.head.commit

print(f"Latest commit is pushed: {latest_local_commit == latest_remote_commit}")

使用os.system()函数,您可以通过python发送shell命令:

import os

os.system('git status')

如果要管理输出,可以使用os.popen()

import os

stream = os.popen('git status')
output = stream.read()

并在output中查找shell打印

相关问题 更多 >