Question

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) ?

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top