views:

49

answers:

4

Imagine, I have several branches: master, a, b, c ...

Now I'm in master branch and "git pull". This fetches all changes from remote server into origin/master, origin/a, origin/b ... branches and merges CURRENT branch (master) with origin/master. But then I want to switch to A branch and again merge these remote changes from remote tracked branch, WITHOUT fetching changes again (as they already are in origin/a branch).

Is there some simplier way to do this not specifying exactly remote branch I track (i.e. automatically) ?

+1  A: 

I think you can simply checkout your local branch and merge from your local copy of the origin/a branch.

Something like:

git checkout a
git merge origin/a
Guillaume Bodi
I'm pretty sure this is no answer at all - see my comment on the question. Don't want to downvote though, as I could be wrong.
Jefromi
Well to me it seems he just doesn't want to reach the server again. He states "WITHOUT fetching changes again (as they already are in origin/a branch)." So the part he doesn't want to do is fetching. Although I may have understood it wrong as well. Best leave it to the OP to decide or give more details. :)
Guillaume Bodi
A: 

git pull is nothing more than git fetch + git merge. With the parameter --rebase you can also have it rebase and not merge so you can just switch to the other branch and merge/rebase:

git checkout a
git merge origin/a

or:

git checkout a
git rebase a

Since it doesn't do anything else I even have stopped using pull at all. I just git fetch regularly so I can track the changes in other repositories easily and then git merge/rebase whenever I'm ready.

Tomas Markauskas
I'm pretty sure this is no answer at all - see my comment on the question. Don't want to downvote though, as I could be wrong.
Jefromi
A: 

I don't believe there's a built-in command for this. However, you could do something like this:

#!/bin/bash
head=$(git symbolic-ref HEAD) || exit
head=${head#refs/heads/}
merge=$(git config --get branch.$head.merge) || { echo "no tracking branch"; exit 1; }
remote=$(git config --get branch.$head.remote) || remote=origin
git merge $remote/${merge#refs/heads/}

# alternatively, as in Aristotle's answer:
# head=$(git symbolic-ref HEAD) || exit
# upstream=$(git for-each-ref --format='%(upstream:short)' "$head"
# [ -z "$upstream" ] && { echo "no tracking branch"; exit 1; }
# git merge $upstream

I think I've covered my bases pretty well - it exits with failure status if in detached HEAD state, or if the current branch doesn't have a tracking branch. It defaults the to origin, as do the normal git transfer commands. (Something weird will happen if you try to use it on a branch which is tracking something other than a branch on the remote, i.e. not of the form refs/heads/*, but that seems pretty unlikely.) Doesn't seem like it'd actually save you much time, but there you are!

If you want to use it, just store it somewhere and alias to it, or name it git-something and put it in your PATH.

Jefromi
Thanks. I will check
Zebooka
A: 

This will check out all tracking branches in turn, and merge in their corresponding remote branches:

git for-each-ref --format='%(refname:short) %(upstream:short)' \
| while read branch upstream ; do
    [ -z "$upstream" ] && continue
    git checkout "$branch"
    git merge "$upstream" || git reset --hard
done

If a merge has conflicts, it is rewound with git reset and you will have to do it manually.

Untested.

You can also pass one or more patterns to git for-each-ref to restrict it to a certain branch or a certain set of them.

Aristotle Pagaltzis
This is dangerous. If `git merge` refuses to try to merge, because the work tree has modifications, the `reset --hard` will destroy those modifications. `reset --merge` would be safer, but still not totally safe. (And I know you have a checkout there, but with local modifications, it'd either have failed or left them in place while switching branches - either way, there are still local modifications.)
Jefromi
Dangerous, but seems to be a hint how to make auto update current branch. Thanks
Zebooka