2

I want to know if there is any way to auto-commit changes made in git based on specific time.

Suppose if I set the configuration, it should commit whatever the code present in repository at exactly 12:00 AM every day or at specific time in a day.

From what I found after searching, there is a way to commit on every time we save a file. But not timely auto-commits.

Sukumar
  • 43
  • 5
  • 1
    Possible duplicate of [Making git auto-commit](https://stackoverflow.com/questions/420143/making-git-auto-commit) – Oguz Ozcan Nov 21 '17 at 18:43
  • I don't need to record all the changes everytime ( which causes more commits ). but all the changes at specific time ( which causes few commits ) – Sukumar Nov 21 '17 at 18:51
  • Build a script and start it in crontab is one way. I do not know if there is a git scheduler...? – Nic3500 Nov 21 '17 at 19:34

2 Answers2

2

As Nic5300 suggested, an easy way to do this is to write a simple script that is called by cron at a specific time:

auto_commit.sh
=======================================
#!/bin/bash
MESSAGE="Auto-commit: $(date)"
REPO_PATH="/home/user/repo"
git -C "$REPO_PATH" add -A
git -C "$REPO_PATH" commit -m "$MESSAGE"

Just update the REPO_PATH and MESSAGE with whatever you'd like. Now, you add the script to your crontab by running crontab -e.

To run it every night at midnight, your crontab would look like this:

* 0 * * * auto_commit.sh > /dev/null 2>&1

Obviously, you'd have to update that path to wherever your script is saved. Just make sure you have cron running (depends on what init system you're using), and you should be good to go. Check out https://crontab-generator.org if you want to fiddle more with your crontab.

John Moon
  • 924
  • 6
  • 10
1

Your crontab example would start executing the auto_commit.sh script at midnight every minute for 1 hour. To make it run only once every midnight, you need:

   0 0 * * * auto_commit.sh > /dev/null 2>&1
EnoTy
  • 11
  • 2