python - 无法使用 Python(子进程)进行 Git 推送?
问题描述
我有这个脚本应该将脚本当前目录中的文件推送到存储库:
def pushToGit():
currDir = os.path.dirname(os.path.realpath(sys.argv[0]))
try:
shutil.rmtree(os.path.join(currDir, '.git'))
except:
pass
try:
cp = cmd.run("git init", check=True, shell=True, cwd=currDir)
cp = cmd.run(f"git remote add origin git@github.com:johnsmith/repo_hold.git", check=True, shell=True, cwd=currDir + "//")
cp = cmd.run("git config user.name 'john smith'", check=True, shell=True, cwd=currDir + "//")
cp = cmd.run("git config user.email 'john@smith.com'", check=True, shell=True, cwd=currDir + "//")
cp = cmd.run("git add .", check=True, shell=True, cwd=currDir + "//")
message = f"Some generated message here"
cp = cmd.run(f"git commit -m '{message}'", check=True, shell=True, cwd=currDir + "//")
cp = cmd.run("git push -u origin master", check=True, shell=True, cwd=currDir + "//")
return True
except Exception as e:
return False
过程很简单:只需初始化文件夹、添加配置、添加文件、提交消息并推送。
在这个例子中,我尝试使用 SSH 方法,因为它会自动运行,所以它不能输入密码。SSH 密钥在同一个文件夹中(为了举例)并附加到我的 GitHub 帐户。
但是,我遇到以下错误:
* Running on http://127.0.0.1:2897/ (Press CTRL+C to quit)
Initialized empty Git repository in C:/Users/John/Desktop/my_repo/.git/
warning: LF will be replaced by CRLF in id_rsa.
The file will have its original line endings in your working directory
error: pathspec 'URL' did not match any file(s) known to git
error: pathspec 'added'' did not match any file(s) known to git
127.0.0.1 - - [08/Sep/2020 22:13:36] "←[37mGET /<url post request here> HTTP/1.1←[0m" 200 -
我不确定是什么导致了问题。我不明白控制台中的错误。这是我第一次使用 SSH 方法,但我不知道我会如何错误地使用它。
如果有帮助,这就是目录中文件的样子:
我究竟做错了什么?
解决方案
首先,在您的命令之间添加一些回声以隔离触发错误的命令。
二、加个git status
检查:
- 如果您在正确的工作目录中
- 文件的状态
类似的错误pathspec 'added'' did not match any file(s) known to git
似乎表明它正在尝试使用先前命令的输出添加或提交文件。
推荐阅读
- c++ - 关于使用 C++11 标准编译 C++98 代码
- python - 如何使用替换功能来完全替换字符串而不是它的一部分?
- python - 计算天数,但从给定小时开始
- java - 为网站提取 SSL 证书信息时堆内存消耗高
- c# - 即使在我创建了一个新实例之后,Syncfusion 的 Masked Edit 也会继续将旧值附加到新值
- office365 - Office-ui-fabric-core 一些图标没有显示
- git - Ubuntu18.04 中的 kTechlab
- javascript - html表jquery中下一行td的断字
- javascript - 如何在 React-quill 中添加自定义字体大小
- javascript - 完成所有承诺后如何使用 setTimeout()?