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

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

    It looks like many others have contributed similar solutions, but I thought I'd share what I came up with and invite others to contribute. This solution has a nice colorful output, gracefully handles your current working directory, and is fast because it doesn't do any checkouts, and leaves your working directory in tact. Also, it is just a shell script with no dependencies other than git. (only tested on OSX so far)

    #!/usr/bin/env bash
    
    gitup(){    
    RED='\033[33;31m'
    YELLO='\033[33;33m'
    GREEN='\033[33;32m'
    NC='\033[0m' # No Color
    
    HEAD=$(git rev-parse HEAD)
    CHANGED=$(git status --porcelain | wc -l)
    
    echo "Fetching..."
    git fetch --all --prune &>/dev/null
    for branch in `git for-each-ref --format='%(refname:short)' refs/heads`; do
    
        LOCAL=$(git rev-parse --quiet --verify $branch)
        if [ "$HEAD" = "$LOCAL" ] && [ $CHANGED -gt 0 ]; then
            echo -e "${YELLO}WORKING${NC}\t\t$branch"
        elif git rev-parse --verify --quiet $branch@{u}&>/dev/null; then
            REMOTE=$(git rev-parse --quiet --verify $branch@{u})
            BASE=$(git merge-base $branch $branch@{u})
    
            if [ "$LOCAL" = "$REMOTE" ]; then
               echo -e "${GREEN}OK${NC}\t\t$branch" 
            elif [ "$LOCAL" = "$BASE" ]; then
                if [ "$HEAD" = "$LOCAL" ]; then
                    git merge $REMOTE&>/dev/null
                else
                    git branch -f $branch $REMOTE
                fi
                echo -e "${GREEN}UPDATED${NC}\t\t$branch"
            elif [ "$REMOTE" = "$BASE" ]; then
                echo -e "${RED}AHEAD${NC}\t\t$branch"
            else
                echo -e "${RED}DIVERGED${NC}\t\t$branch"
            fi
        else
            echo -e "${RED}NO REMOTE${NC}\t$branch"
        fi
    done
    }
    

    https://github.com/davestimpert/gitup

    Sorry I also seem to have come up with the same name as the other tool above.

提交回复
热议问题