要通过 HEAD 访问所有 git 提交,可以使用 git log 命令来遍历并获取所有提交的信息。以下是一个示例代码,使用 Python 语言调用 git log 命令来遍历所有提交并打印提交信息:
import subprocess
# 使用 subprocess 调用 git log 命令
git_log_command = 'git log --oneline'
output = subprocess.check_output(git_log_command, shell=True).decode("utf-8")
# 按行分割输出结果
commits = output.strip().split('\n')
# 遍历每个提交
for commit in commits:
# 分割提交 hash 和提交信息
commit_parts = commit.split(' ', 1)
commit_hash = commit_parts[0]
commit_message = commit_parts[1]
# 打印提交信息
print(f'Commit: {commit_hash}')
print(f'Message: {commit_message}')
print('---')
这段代码通过调用 git log --oneline 命令获取所有提交的简洁信息,并按行分割输出结果。然后,它遍历每个提交,分割提交 hash 和提交信息,并打印出来。
请确保在运行代码之前,所在的目录是一个 git 仓库。另外,需要在代码中指定正确的 git log 命令,以适应你的需求。