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

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

    The script from @larsmans, a bit improved:

    #!/bin/sh
    
    set -x
    CURRENT=`git rev-parse --abbrev-ref HEAD`
    git fetch --all
    for branch in "$@"; do
      if ["$branch" -ne "$CURRENT"]; then
        git checkout "$branch" || exit 1
        git rebase "origin/$branch" || exit 1
      fi
    done
    git checkout "$CURRENT" || exit 1
    git rebase "origin/$CURRENT" || exit 1
    

    This, after it finishes, leaves working copy checked out from the same branch as it was before the script was called.

    The git pull version:

    #!/bin/sh
    
    set -x
    CURRENT=`git rev-parse --abbrev-ref HEAD`
    git fetch --all
    for branch in "$@"; do
      if ["$branch" -ne "$CURRENT"]; then
        git checkout "$branch" || exit 1
        git pull || exit 1
      fi
    done
    git checkout "$CURRENT" || exit 1
    git pull || exit 1
    

提交回复
热议问题