Check if pull needed in Git

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

    I use a version of a script based on Stephen Haberman's answer:

    if [ -n "$1" ]; then
        gitbin="git -C $1"
    else
        gitbin="git"
    fi
    
    # Fetches from all the remotes, although --all can be replaced with origin
    $gitbin fetch --all
    if [ $($gitbin rev-parse HEAD) != $($gitbin rev-parse @{u}) ]; then
        $gitbin rebase @{u} --preserve-merges
    fi
    

    Assuming this script is called git-fetch-and-rebase, it can be invoked with an optional argument directory name of the local Git repository to perform operation on. If the script is called with no arguments, it assumes the current directory to be part of the Git repository.

    Examples:

    # Operates on /abc/def/my-git-repo-dir
    git-fetch-and-rebase /abc/def/my-git-repo-dir
    
    # Operates on the Git repository which the current working directory is part of
    git-fetch-and-rebase
    

    It is available here as well.

提交回复
热议问题