如何正确添加' git push'命令crontab - Ubuntu

时间:2017-02-21 13:22:43

标签: git ubuntu cron crontab

我认为这是一个简单的问题,但我已将Origin Master添加为this guide oriented

我创建了一个文件backupgit.cron,并在其中包含以下命令:

cd /var/bkpfolder/
git add .
git commit -m "another commit"
git push origin master

crontab -e我包括了以下行:

30 * * * * /var/bkpfolder/backupgit.cron

问题是,我添加的用于测试此crontab的(新)文件永远不会上传...如果我在backupgit.cron手动运行命令,它们就可以正常工作。

我认为这与进入origin master文件夹(cd /var/bkpfolder/?)执行推送命令有关,所以我正在寻找的是< strong>执行/bkpfolder/

中所述的backupgit.cron 的推送命令的正确方法

提前感谢!

2 个答案:

答案 0 :(得分:0)

您的来源master必须是裸存储库,否则可能无法检出分支。

如果它不是裸存储库并且检出了将要推送的分支,则会出现以下错误:

git push origin master --force
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set 'receive.denyCurrentBranch' configuration variable to
remote: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /Users/Roman/gittest/master
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/Users/Roman/gittest/master'

解决方案

如果您不想使用裸存储库,则必须签出在bkpfoldermaster目录之间不同的分支。

开箱即用的示例

为了更好的未定,我将远程master重命名为origin

mkdir main_backup
cd main_backup && git init --bare && cd ..
git clone main_backup bkpfolder
cd bkpfolder && echo "This is a test" >> test.txt
git add .
git commit -m 'Automatic backup'
git push origin master

答案 1 :(得分:0)

我弄清楚我做错了什么......正如我所说,backupgit.cron里面的命令在我进入文件夹时工作(读作不是默认文件夹crontab)...所以我刚刚发现在git命令的开始时我可以指定我的行动文件夹,如git -C /var/bkpfolder/

所以我替换了backupgit.cron 中的内容:

cd /var/bkpfolder/
git add .
git commit -m "another commit"
git push origin master

git -C /var/bkpfolder/ add .
git -C /var/bkpfolder/ commit -m "another commit"
git -C /var/bkpfolder/ push origin master

<强> +

添加了读取/写入crontab写入

的用户的文件的权限