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

前端 未结 25 2481
失恋的感觉
失恋的感觉 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:19

    To complete the answer by Matt Connolly, this is a safer way to update local branch references that can be fast-forwarded, without checking out the branch. It does not update branches that cannot be fast-forwarded (i.e. that have diverged), and it does not update the branch that is currently checked out (because then the working copy should be updated as well).

    git fetch
    
    head="$(git symbolic-ref HEAD)"
    git for-each-ref --format="%(refname) %(upstream)" refs/heads | while read ref up; do
        if [ -n "$up" -a "$ref" != "$head" ]; then
            mine="$(git rev-parse "$ref")"
            theirs="$(git rev-parse "$up")"
            base="$(git merge-base "$ref" "$up")"
            if [ "$mine" != "$theirs" -a "$mine" == "$base" ]; then
                git update-ref "$ref" "$theirs"
            fi
        fi
    done
    

提交回复
热议问题