Check if pull needed in Git

后端 未结 24 1337
忘了有多久
忘了有多久 2020-11-22 13:34

How do I check whether the remote repository has changed and I need to pull?

Now I use this simple script:

git pull --dry-run | grep -q -v \'Already          


        
24条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 14:12

    Maybe this, if you want to add task as crontab:

    #!/bin/bash
    dir="/path/to/root"
    lock=/tmp/update.lock
    msglog="/var/log/update.log"
    
    log()
    {
            echo "$(date) ${1:-missing}" >> $msglog
    }
    
    if [ -f $lock ]; then
            log "Already run, exiting..."
    else
            > $lock
            git -C ~/$dir remote update &> /dev/null
            checkgit=`git -C ~/$dir status`
            if [[ ! "$checkgit" =~ "Your branch is up-to-date" ]]; then
                    log "-------------- Update ---------------"
                    git -C ~/$dir pull &>> $msglog
                    log "-------------------------------------"
            fi
            rm $lock
    
    fi
    exit 0
    

提交回复
热议问题