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

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

    A script I wrote for my GitBash. Accomplishes the following:

    • By default pulls from origin for all branches that are setup to track origin, allows you to specify a different remote if desired.
    • If your current branch is in a dirty state then it stashes your changes and will attempt to restore these changes at the end.
    • For each local branch that is set up to track a remote branch will:
      • git checkout branch
      • git pull origin
    • Finally, will return you to your original branch and restore state.

    ** I use this but have not tested thoroughly, use at own risk. See an example of this script in a .bash_alias file here.

        # Do a pull on all branches that are tracking a remote branches, will from origin by default.
        # If current branch is dirty, will stash changes and reply after pull.
        # Usage: pullall [remoteName]
        alias pullall=pullAll
        function pullAll (){
         # if -h then show help
         if [[ $1 == '-h' ]]
        then
          echo "Description: Pulls new changes from upstream on all branches that are tracking remotes."
          echo 
          echo "Usage: "
          echo "- Default: pullall"
          echo "- Specify upstream to pull from: pullall [upstreamName]"
          echo "- Help: pull-all -h"
        else
    
         # default remote to origin
         remote="origin"
         if [ $1 != "" ]
         then
           remote=$1
         fi
    
         # list all branches that are tracking remote
         # git branch -vv : list branches with their upstreams
         # grep origin : keep only items that have upstream of origin
         # sed "s/^.."... : remove leading *
         # sed "s/^"..... : remove leading white spaces
         # cut -d" "..... : cut on spaces, take first item
         # cut -d splits on space, -f1 grabs first item
         branches=($(git branch -vv | grep $remote | sed "s/^[ *]*//" | sed "s/^[ /t]*//" | cut -d" " -f1))
    
         # get starting branch name
         startingBranch=$(git rev-parse --abbrev-ref HEAD)
    
         # get starting stash size
         startingStashSize=$(git stash list | wc -l)
    
         echo "Saving starting branch state: $startingBranch"
         git stash
    
         # get the new stash size
         newStashSize=$(git stash list | wc -l)
    
         # for each branch in the array of remote tracking branches
         for branch in ${branches[*]}
         do
           echo "Switching to $branch"
           git checkout $branch
    
           echo "Pulling $remote"
           git pull $remote
    
         done
    
         echo "Switching back to $startingBranch"
         git checkout $startingBranch
    
         # compare before and after stash size to see if anything was stashed
         if [ "$startingStashSize" -lt "$newStashSize" ]
         then
           echo "Restoring branch state"
           git stash pop
         fi
        fi
        }
    

提交回复
热议问题