tags:

views:

170

answers:

4

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?

+2  A: 

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.

Greg Hewgill
A: 

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.
Alex Brown
You can do that for a `git push`, but can you do the same for a `git pull`?
Greg Hewgill
Sort of. You can tell it to fetch and update all remote tracking branches, and pull the current branch. The other branches you have will need to be manually merged. Note that a pull on a branch branched from a remote tracking branch will merge from the remote tracking branch.
Alex Brown
And the man page claims you can do the real deal, too. I've updated the post.
Alex Brown
A: 

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
This may need manual intervention if there are any conflicts in the merge that cannot be automatically resolved.
Greg Hewgill
Problem with this is I will have to modify this every time I add new branches.
cool-RR
A: 

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.

Squelch