如何正确地将 'git push' 命令添加到 crontab - Ubuntu
How to correctly add 'git push' command to crontab - Ubuntu
我认为这是一个简单的问题,但我添加了一个文件夹 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/
?)执行推送命令有关,所以我要找的是 对 backupgit.cron
中规定的 /bkpfolder/
执行推送命令的正确方法
提前致谢!
您的来源 master
必须是 bare-repository 否则分支可能无法签出。
如果它不是 bare-repository 并且将被推送的分支被签出,您将收到以下错误:
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'
解决方案
如果您不想使用 bare-repository,您必须签出 bkpfolder
和 master
目录不同的分支。
开箱即用示例
为了更好地理解,我将遥控器 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
我弄清楚我做错了什么......正如我所说,当我在文件夹内时 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
+
添加了对 read/write 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/
?)执行推送命令有关,所以我要找的是 对 backupgit.cron
/bkpfolder/
执行推送命令的正确方法
提前致谢!
您的来源 master
必须是 bare-repository 否则分支可能无法签出。
如果它不是 bare-repository 并且将被推送的分支被签出,您将收到以下错误:
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'
解决方案
如果您不想使用 bare-repository,您必须签出 bkpfolder
和 master
目录不同的分支。
开箱即用示例
为了更好地理解,我将遥控器 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
我弄清楚我做错了什么......正如我所说,当我在文件夹内时 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
+
添加了对 read/write crontab 写入用户的文件的权限