0

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
3

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.

0
#!/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
2
  • 3
    It would be nice to wrap your question in some explanatory sentences. – zx485 Feb 15 '20 at 1:48
  • 1
    a shorter way is if git status | grep -q modified – dessert Feb 15 '20 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.