首页 > 解决方案 > 使用命令提示符下的 python 脚本从 git bash 克隆 git repo 失败。为什么?

问题描述

git bash 突然关闭而不克隆 repo。我无法理解这里有什么问题。

import os
import subprocess
    
parent_dir = r'C:\Users\user\Documents'
dir_name = 'Git_temp'
dir_path = os.path.join(parent_dir, dir_name)
    
os.mkdir(dir_path)
print("{0} created under {1}".format(dir_name, parent_dir))
os.chdir(dir_path)
print("Current Working Directory : {0}".format(os.getcwd()))
git_file = "C:\Program Files\Git\git-bash.exe"
git_clone_cmd = "git clone https://github.com/patankar-saransh/test_repo.git"
subprocess.Popen([git_file, git_clone_cmd])

标签: pythongitpython-2.7github

解决方案


如果您不想使用GitPython,那么只需确保git.exe在您的%PATH%.

那么您的电话将是:

import subprocess
process = subprocess.Popen(["git", "clone", "https://..."], stdout=subprocess.PIPE)
output = process.communicate()[0]

如此处所示,对于 Python 2.7+,使用check_output

import subprocess
output = subprocess.check_output(["git",  "clone", "https://..."]])

推荐阅读