Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to merge local branch with remote tracking automatically without fetching

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

like image 895
Zebooka Avatar asked Nov 06 '22 07:11

Zebooka


1 Answers

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.

like image 106
Cascabel Avatar answered Nov 15 '22 05:11

Cascabel