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

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

    If you're on Windows you can use PyGitUp which is a clone of git-up for Python. You can install it using pip with pip install --user git-up or through Scoop using scoop install git-up

    [4]

    0 讨论(0)
  • 2020-11-22 17:34

    Just posting an updated answer. git-up is no longer maintained and if you read the documentation, they mention the functionality is now available in git.

    As of Git 2.9, git pull --rebase --autostash does basically the same thing.

    Accordingly, if you update to Git 2.9 or later, you can use this alias instead of installing git-up:

    git config --global alias.up 'pull --rebase --autostash'

    You can also set this for every git pull as of Git 2.9 as well (thanks @VonC please see his answer here)

    git config --global pull.rebase true
    git config --global rebase.autoStash true
    
    0 讨论(0)
  • 2020-11-22 17:34

    In fact, with git version 1.8.3.1, it works:

    [root@test test]# git br
    * master
      release/0.1
      update
    [root@test test]# git pull --rebase
    remote: Enumerating objects: 9, done.
    remote: Counting objects: 100% (9/9), done.
    remote: Compressing objects: 100% (9/9), done.
    remote: Total 9 (delta 2), reused 0 (delta 0)
    Unpacking objects: 100% (9/9), done.
    From http://xxx/scm/csdx/test-git
       d32ca6d..2caa393  release/0.1 -> origin/release/0.1
    Current branch master is up to date.
    [root@test test]# git --version
    git version 1.8.3.1
    

    In master branch, you can update all other branches. @Cascabel

    I do not know which version break/fix it, in 2.17(which i use), it can work.

    0 讨论(0)
  • 2020-11-22 17:35

    I use the sync subcommand of hub to automate this. I have alias git=hub in my .bash_profile, so the command I type is:

    git sync
    

    This updates all local branches that have a matching upstream branch. From the man page:

    • If the local branch is outdated, fast-forward it;
    • If the local branch contains unpushed work, warn about it;
    • If the branch seems merged and its upstream branch was deleted, delete it.

    It also handles stashing/unstashing uncommitted changes on the current branch.

    I used to use a similar tool called git-up, but it's no longer maintained, and git sync does almost exactly the same thing.

    0 讨论(0)
  • 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
        }
    
    0 讨论(0)
  • 2020-11-22 17:36

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

    No it cannot. For fast-forwarding, I just wrote a small tool to do so. https://github.com/changyuheng/git-fast-forward-all

    Advantages of this tool:

    1. Supports multiple remotes in one repository. (hub sync doesn't support multiple remotes at the moment.)
    2. Supports having different names on the local branch and the corresponding remote tracking branche.
    3. Much faster than other scripts that fetches remote for every single branch.
    4. No error-prone regex parsing/editing.
    0 讨论(0)
提交回复
热议问题