4
votes

I have a git repo in GitHub. I've been mostly using git gui to manage it up to now, but now I'm starting to use the shell. What I want to know is, how do I fetch all the branches in a remote and merge them into the respective branches on my local repo automatically? For example, if I have branches master and development, and thus origin/master and origin/development, I want to fetch origin/master and merge it into master and to fetch origin/development and merge it into development.

Is there a command that does this?

4

4 Answers

4
votes

There isn't a single command that will fetch and merge all branches simultaneously. The git pull command:

Runs git-fetch with the given parameters, and calls git-merge to merge the retrieved head(s) into the current branch.

So it only works on the current branch. This is necessary because you need to be in a position to deal with conflicts when they arise.

2
votes

To list the local branches:

$ git branch
*  master

To list available remote branches:

$ git branch -r
* master
  branch1
  branch2

Now it gets interesting, To track a remote branch1:

$ git branch --track branch1

This enables a pull to also update the branch locally.

Simply switch to the new branch with:

$ git checkout branch1

By using the --track parameter, the remote branches are added to .git/config and will become persistent. ie this only needs to be done once. The pertinent entries from .git/config will show like this:

branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.branch1.remote=origin
branch.branch1.merge=refs/heads/branch1

By running branch --track commands for each of the remote branches you would like to have locally will do exactly what you want.

0
votes

If you know the branch names in question, you can set this up so it happens on a

git pull

by editing the .git/config file.

See

man git-pull 

for more details: (cloned from man git-pull below)

   o   Command line pull of multiple branches from one repository:

           $ git checkout master
           $ git fetch origin +pu:pu maint:tmp
           $ git pull . tmp

       This updates (or creates, as necessary) branches pu and tmp in the
       local repository by fetching from the branches (respectively) pu
       and maint from the remote repository.

       The pu branch will be updated even if it is does not fast-forward;
       the others will not be.
0
votes

Put the following in a file named pull-all:

#!/bin/bash
git fetch origin
git checkout development
git merge origin/development
git checkout master
git merge origin/master

Then, give pull-all executable permissions and put it into a directory on your path. Usually, $HOME/bin:

chmod +x pull-all
mv pull-all $HOME/bin