Check if pull needed in Git

后端 未结 24 1291
忘了有多久
忘了有多久 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:08

    All such complex sugestions while the solution is so short and easy:

    #!/bin/bash
    
    BRANCH="<your branch name>"
    LAST_UPDATE=`git show --no-notes --format=format:"%H" $BRANCH | head -n 1`
    LAST_COMMIT=`git show --no-notes --format=format:"%H" origin/$BRANCH | head -n 1`
    
    git remote update
    if [ $LAST_COMMIT != $LAST_UPDATE ]; then
            echo "Updating your branch $BRANCH"
            git pull --no-edit
    else
            echo "No updates available"
    fi
    
    0 讨论(0)
  • 2020-11-22 14:11

    If you have an upstream branch

    git fetch <remote>
    git status
    

    If you don't have an upstream branch

    Compare the two branches:

    git fetch <remote>
    git log <local_branch_name>..<remote_branch_name> --oneline
    

    For example:

    git fetch origin
    
    # See if there are any incoming changes
    git log HEAD..origin/master --oneline
    

    (I'm assuming origin/master is your remote tracking branch)

    If any commits are listed in the output above, then you have incoming changes -- you need to merge. If no commits are listed by git log then there is nothing to merge.

    Note that this will work even if you are on a feature branch -- that does not have a tracking remote, since if explicitly refers to origin/master instead of implicitly using the upstream branch remembered by Git.

    0 讨论(0)
  • 2020-11-22 14:11
    git ls-remote | cut -f1 | git cat-file --batch-check >&-
    

    will list everything referenced in any remote that isn't in your repo. To catch remote ref changes to things you already had (e.g. resets to previous commits) takes a little more:

    git pack-refs --all
    mine=`mktemp`
    sed '/^#/d;/^^/{G;s/.\(.*\)\n.* \(.*\)/\1 \2^{}/;};h' .git/packed-refs | sort -k2 >$mine
    for r in `git remote`; do 
        echo Checking $r ...
        git ls-remote $r | sort -k2 | diff -b - $mine | grep ^\<
    done
    
    0 讨论(0)
  • 2020-11-22 14:12

    If you run this script, it will test if the current branch need a git pull:

    #!/bin/bash
    
    git fetch -v --dry-run 2>&1 |
        grep -qE "\[up\s+to\s+date\]\s+$(
            git branch 2>/dev/null |
               sed -n '/^\*/s/^\* //p' |
                    sed -r 's:(\+|\*|\$):\\\1:g'
        )\s+" || {
            echo >&2 "Current branch need a 'git pull' before commit"
            exit 1
    }
    

    It's very convenient to put it as a Git hook pre-commit to avoid

    Merge branch 'foobar' of url:/path/to/git/foobar into foobar
    

    when you commit before pulling.

    To use this code as a hook, simply copy/paste the script in

    .git/hooks/pre-commit
    

    and

    chmod +x .git/hooks/pre-commit
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 14:13

    If this is for a script, you can use:

    git fetch
    $(git rev-parse HEAD) == $(git rev-parse @{u})
    

    (Note: the benefit of this vs. previous answers is that you don't need a separate command to get the current branch name. "HEAD" and "@{u}" (the current branch's upstream) take care of it. See "git rev-parse --help" for more details.)

    0 讨论(0)
提交回复
热议问题