Can “git pull --all” update all my local branches?

前端 未结 25 2399
失恋的感觉
失恋的感觉 2020-11-22 16:33

I often have at least 3 remote branches: master, staging and production. I have 3 local branches that track those remote branches.

Updating all my local branches is

25条回答
  •  無奈伤痛
    2020-11-22 17:14

    If refs/heads/master can be fast-forwarded to refs/remotes/foo/master, the output of

    git merge-base refs/heads/master refs/remotes/foo/master
    

    should return the SHA1 id that refs/heads/master points to. With this, you can put together a script that automatically updates all local branches that have had no diverting commits applied to them.

    This little shell script (I called it git-can-ff) illustrates how it can be done.

    #!/bin/sh
    
    set -x
    
    usage() {
        echo "usage: $(basename $0)  " >&2
        exit 2
    }
    
    [ $# -ne 2 ] && usage
    
    FROM_REF=$1
    TO_REF=$2
    
    FROM_HASH=$(git show-ref --hash $FROM_REF)
    TO_HASH=$(git show-ref --hash $TO_REF)
    BASE_HASH=$(git merge-base $FROM_REF $TO_REF)
    
    if [ "$BASE_HASH" = "$FROM_HASH" -o \
         "$BASE_HASH" = "$FROM_REF" ]; then
        exit 0
    else
        exit 1
    fi
    

提交回复
热议问题