首页 > 解决方案 > 致命:使用 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. 我该如何解决这个问题?如果我从终端手动运行命令,我没有问题,也不会提示我输入用户凭据。

对此的任何指示将不胜感激。谢谢!

标签: bashgitmacoscron

解决方案


建议使用 SSH 密钥自动执行 git 任务时。您可以按照以下步骤设置具有推送访问权限的 SSH 密钥:

  1. 获取用户的 SSH 密钥(或生成一个ssh-keygen -t rsa -b 4096 -C "your_email@example.com":)
  2. 复制公钥的内容 ( cat ~/.ssh/id_rsa.pub)
  3. 转到 github.com 上的项目
  4. 进入项目设置
  5. 选择Deploy Keyshttps://github.com/user/project/settings/keys
  6. 选择添加部署密钥
  7. 添加您的公钥内容
  8. 查看Allow write access
  9. 选择Add Key

请注意,这仅适用于使用以下遥控器克隆的存储库:git@github.com:user/project.git

可以通过以下步骤更新遥控器:

  1. 转到服务器上的存储库文件夹
  2. git remote remove origin
  3. git remote add origin git@github.user/project.git

推荐阅读