在 shell 中,我输入“git status”并获取输出:
Your branch is up-to-date with 'origin/master'.
Run Code Online (Sandbox Code Playgroud)
或者
Your branch is ahead of 'origin/master' by 3 commits.
Run Code Online (Sandbox Code Playgroud)
在 GitPython 中,我怎样才能知道更改是否尚未推送到远程?
似乎还没有此功能的包装器(我自己搜索了一个包装器并偶然发现了您的答案)。但你可以做的就是直接使用 git。
根据您希望解决方案的复杂程度,一种快速方法可能是执行以下操作:
import re
from git import Repo
a_repo = Repo("/path/to/your/repo")
ahead = 0
behind = 0
# Porcelain v2 is easier to parse, branch shows ahead/behind
repo_status = a_repo.git.status(porcelain="v2", branch=True)
ahead_behind_match = re.search(r"#\sbranch\.ab\s\+(\d+)\s-(\d+)", repo_status)
# If no remotes exist or the HEAD is detached, there is no ahead/behind info
if ahead_behind_match:
ahead = int(ahead_behind_match.group(1))
behind = int(ahead_behind_match.group(2))
print("Repo is {} commits ahead and {} behind.".format(ahead, behind))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
739 次 |
| 最近记录: |