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) ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With