3

I've found some nice questions illustrating how one would write a script automating git push/pull with cron here and here, but I want to elaborate on that slightly. I only want to push/pull if there were changes made that day.

I basically want the script to check the difference log between origin and this repository and only push or pull if there are changes. I'm not very experienced with bash and git, so I don't really know where to start, otherwise I would paste what I've tried so far.

Thank you for any help or guidance.

0

2 Answers 2

5

git push and git pull change your files only when there are differences.

if you try to git add --all when there's no changes, it won't add anything, the subsequent git commit -m "" and git push will have no effect also.

the script you referred should work properly.

if you want extra checks you can do git remote update then git diff to see the difference before merging the local files.

3
#!/bin/bash 
cd projectdir
git add .
set +e  # Grep succeeds with nonzero exit codes to show results.
git status | grep modified
if [ $? -eq 0 ]
then
    set -e
    git commit -am "updated on - $(date)"
    git push
else
    set -e
    echo "No changes since last run"
fi
1
  • 2
    a shorter way is if git status | grep -q modified
    – dessert
    Feb 15, 2020 at 8:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .