从Python获取git分支名称和哈希!


意思是

1
2
3
4
import subprocess
cmd = "git rev-parse --short HEAD"
hash = subprocess.check_output(cmd.split()).strip().decode('utf-8')
print(hash)

python_git_hash.png

  • 有了子流程,我没有迷路...

附录

  • 请注意hash是python的保留字...

附录

获取分支名称

1
2
3
4
import subprocess
_cmd = "git rev-parse --abbrev-ref HEAD"
branch = subprocess.check_output(_cmd.split()).strip().decode('utf-8')
branch = "-".join(branch.split("/"))

  • 请注意,如果您没有适当地替换斜杠,则在从python执行os.mkdir()时会收到错误消息。

参考

  • https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script