1

I have a folder that I want to publicly share with others, the content of the folder suppose to be updated daily: some files gonna be changed, some are deleted, some are added. I would like to use GitHub for that and create a cronjob that supposes to handle the repository updates. What kind of git commands do I have to use if I always want to have an exact copy of the local folder on my git repository.

This is the code that I am planning to use but I have doubts about locally deleted files (if it is cover them or not)‍♂️:

cd ~/myfolder
git init
git add .
git commit -a -m "$current_date"

Thank you.

Ra Spirit
  • 13
  • 3

1 Answers1

0

That code is almost correct for the initial setup of your repository, there's a few more steps you need to add to the end:

git branch -M main
git remote add origin git@github.com:username/repository-name.git #Edit this to match your Github repo
git push -u origin main

The first line I've added sets the branch on git (Github default branch is main), the second line sets the remote origin, and the final line pushes your folder to Github.

The script you call from crontab should look like this:

cd ~/myfolder
git add .
git commit -a -m "$current_date"
git push

Assuming you named it upload-script.sh, this is the crontab entry, which will run at every minute (the fastest interval crontab allows) with no output:

* * * * * ~/upload-script.sh >/dev/null 2>&1

I'm not sure if you can ger rate-limited by Github for this, so you might need to decrease the speed of pushes. You can generate crontab entries using crontab generator if this is the case.

And for reference, here's Github's documentation on how to set up a remote repository (helpful for debugging git issues): https://docs.github.com/en/get-started/using-git/pushing-commits-to-a-remote-repository

Hope this helps :)

  • Thank you very much for your overall answer. I have noticed that the .git folder is used a lot of space. I was doing some research and I found 2 more commands to reduce the size of the folder which is: `git fsck` `git prune` My question is should I use them? as I said I don't need any history of the files. Also, it would be wonderful if there is a way to get the repository without .git folder or with a minimum size. Of course, I can delete the folder after but I am worried about the bandwidth which I would like to save. Thank you! – Ra Spirit Oct 21 '21 at 04:20
  • Is that make sense to make the code like that: `cd ~/myfolder; git fsck; git prune; git add .; git commit -a -m "$current_date"; git push`' – Ra Spirit Oct 21 '21 at 07:37
  • Honestly, if you’re not needing the history then there’s better methods to do this than Git (e.g. cloud storage, localexpose services), but your changes should keep the size of .git to a minimum. Also, Git doesn’t upload the entire repo every time you push, only the files which have changed since the last push (see this question on SO: https://stackoverflow.com/questions/26005031/what-does-git-push-do-exactly ). – Pranav Sharma Oct 22 '21 at 05:11
  • Also I noticed you’re fairly new to SE, so welcome :) if my answer fulfilled what you need, please accept it so that other people can see what helped you. – Pranav Sharma Oct 22 '21 at 05:13