[英]Discovering git branch from python script
I have a python script.我有一个 python 脚本。 I need to access the name of the git branch from which I'm running the python script, through python, during runtime.
我需要在运行时通过 python 访问 git 分支的名称,我从该分支运行 python 脚本。 Is there a way to do this?
有没有办法做到这一点?
Edit: os.system("git rev-parse --abbrev-ref HEAD") outputs to the cli, I don't see how I would get access to it from python...编辑: os.system("git rev-parse --abbrev-ref HEAD") 输出到 cli,我不知道如何从python 访问它...
I would like to have sth like git_branch = <python commands>
我想要像
git_branch = <python commands>
这样的东西
You could use GitPython , something like the following:您可以使用GitPython ,如下所示:
>>> import git
>>> import os
>>> git_branch = git.Repo(os.getcwd()).active_branch.name
>>> git_branch
'master'
Otherwise, as already pointed out by Yevhen Kuzmovych in the comments, you could use PyGit .否则,正如 Yevhen Kuzmovych 在评论中已经指出的那样,您可以使用PyGit 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.