bash - 致命:使用 cron 作业推送到 git 时无法读取用户名
问题描述
嘿,我正在尝试使用 Mac 上的 cron 作业将代码推送到 git。我使用编辑我的 crontabcrontab -e
并在里面有以下内容:
* 12 * * 1 ~/Dropbox/MD/sync.sh
* 12 * * 5 ~/Dropbox/MD/sync.sh
脚本如下:
#!/bin/bash
cd ~/Dropbox/MD
/usr/bin/git add .
/usr/bin/git commit -m "Docs auto update"
/usr/bin/git push origin master
但是命令失败,当我运行时,mail
我可以检查错误消息,即:fatal: could not read Username for 'https://github.com': Device not configured
. 我该如何解决这个问题?如果我从终端手动运行命令,我没有问题,也不会提示我输入用户凭据。
对此的任何指示将不胜感激。谢谢!
解决方案
建议使用 SSH 密钥自动执行 git 任务时。您可以按照以下步骤设置具有推送访问权限的 SSH 密钥:
- 获取用户的 SSH 密钥(或生成一个
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
:) - 复制公钥的内容 (
cat ~/.ssh/id_rsa.pub
) - 转到 github.com 上的项目
- 进入项目设置
- 选择
Deploy Keys
(https://github.com/user/project/settings/keys) - 选择添加部署密钥
- 添加您的公钥内容
- 查看
Allow write access
- 选择
Add Key
请注意,这仅适用于使用以下遥控器克隆的存储库:git@github.com:user/project.git
可以通过以下步骤更新遥控器:
- 转到服务器上的存储库文件夹
- 跑
git remote remove origin
- 跑
git remote add origin git@github.user/project.git
推荐阅读
- node.js - 验证现有用户的推特
- java - springboot中的caffeine缓存是否有可能由于缓存的数据过多而导致500或超时错误?
- asp.net-core-mvc - 如何在 ASP.NET Core MVC 中使用 DbContext.FindAsync() 读取文本[]?
- azure - 如何更改命名空间 kube-system 的 CPU 限制
- python - python - 具有嵌套结构的多个文件夹
- python - 即使调用一次,Keras Lambda 层也会运行两次
- r - 有条件地保留和删除 R 中的行
- python - Dividing a list into subsets according to a numeric array in Python
- angular - 属性在规范文件中被识别为未定义,即使它存在于组件中。使用 Karma 进行 Angular 单元测试 - Jasmine
- python - Pybullet:如何获取 getJointStates 和 setJointMotorControlArray(TORQUE_CONTROL) 之间的扭矩值关系?