Python 如何获取任何存储库的sha密钥';文件

Python 如何获取任何存储库的sha密钥';文件,python,git,gitpython,Python,Git,Gitpython,我想单独访问任何存储库文件的sha密钥 如你所见。有一个文件结构,其中包括我用gitpython创建的.git目录。我可以获得存储库sha密钥来表示所有文件,如下所示 from git import Repo repo = Repo("graph/features") master = repo.head.reference print(master.commit.hexsha) 我的问题是,我想为任何文件(如file2.txt)生成sha密钥。有没有办法实现这一点。很抱歉,我对使用git

我想单独访问任何存储库文件的sha密钥

如你所见。有一个文件结构,其中包括我用gitpython创建的.git目录。我可以获得存储库sha密钥来表示所有文件,如下所示

from git import Repo

repo = Repo("graph/features")
master = repo.head.reference
print(master.commit.hexsha)
我的问题是,我想为任何文件(如file2.txt)生成sha密钥。有没有办法实现这一点。很抱歉,我对使用gitPython不太熟悉。

那么请阅读文件

>>> import hashlib
>>> file_text = 'content'
>>> hashlib.sha256(file_text.encode()).hexdigest()
'ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73'
>>> 
路径:存储库(.git)路径

文件名:要添加哪个文件

消息:提交消息


之后采用休耕法

repo = Repo(path)
commit = repo.head.commit

sha = commit.hexsha

事实上,这种方法不是我想要的,但我会使用它。非常感谢您的关注
repo = Repo(path)
commit = repo.head.commit

sha = commit.hexsha