如何使用 pygit2 进行初始提交?

How to make an initial commit using pygit2?

使用 pygit2 包如何在新存储库上创建初始提交?

我不断收到一条错误消息,提示找不到“refs/heads/master”。

在某些情况下,我正在研究一个 Python 模块,该模块是从我用来管理 GitLab 实例到 post 的当前意大利面条代码创建的,并对大学课程的作业进行评分。可以找到工作 here。我的应用程序要求我为所有学生下载一个存储库作为分配给 post 的“模板”,因此我需要为我课程中的所有学生使用具有新 git 存储库的相同文件树(因此需要一遍又一遍地创建初始提交)。

这里的技巧是将第一个参数设置为 create_commit"HEAD" 而不是示例中的 "refs/heads/master"。也许这对那里的 git 巫师来说是显而易见的,但我花了一秒钟才意识到。

import pygit2 as git

template_proj_location = "/file/path/to/the/template"
# 'proj' is a python-gitlab object for the GitLab project this repo needs to get pushed to

repo = git.init_repository(template_proj_location, initial_head='master', origin = proj.ssh_url_to_repo)
(index := repo.index).add_all()
index.write()
tree = index.write_tree()
me = git.Signature("My Name", "myemail@email.domain.edu")
repo.create_commit("HEAD", me, me, "commit msg", tree, [])

# Now to push the repo to the new location
_, ref = repo.resolve_refish(refish=repo.head.name)
remote = repo.remotes["origin"]
remote.push([ref.name], callbacks = my_func_to_get_RemoteCallbacks_obj_for_ssh_auth)

我参考了以下内容: