以下是一个示例代码,展示了如何通过Git命令来获取历史记录,而无需签出任何文件:
import subprocess
def get_git_history():
# 使用Git命令获取提交历史记录
cmd = "git log --oneline"
output = subprocess.check_output(cmd.split()).decode("utf-8")
# 将输出结果按行拆分为列表
history = output.strip().split("\n")
return history
# 调用函数获取历史记录
history = get_git_history()
# 打印历史记录
for commit in history:
print(commit)
这个示例代码使用Python的subprocess
模块来执行Git命令,并将输出结果保存在变量output
中。然后,我们将输出结果按行拆分为列表,并将该列表作为函数的返回值。
在这个示例中,我们使用git log --oneline
命令来获取提交历史记录的简洁版本。你也可以根据自己的需求来修改Git命令,以获取更详细或特定的历史记录。