Git: how to combine local branch with remote tracking automatically without fetching

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

Now I am in the main branch and "git pull". This fetches all changes from the remote server to the source / master, origin / a, origin / b ... branches and merges the CURRENT (master) branch named origin / master. But then I want to switch to branch A and merge these remote changes from the remote tracked branch again, WITHOUT fetching the changes again (since they are already in the beginning / branch).

Is there an easier way to do this without specifying the exact remote branch I will track (that is, automatically)?

+4
source share
4 answers

I do not think there is a built-in command for this. However, you can 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 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 covered my bases well - it exits with a failure status if it is in the offline state of HEAD, or if the current branch does not have a tracking branch. By default, the source is used, as are the usual git transfer commands. (Something strange will happen if you try to use it on a branch that tracks something other than the branch on the remote, that is, not in the form of refs / heads / *, but that seems unlikely.) It doesn't seem like it 'd actually save you a lot of time, but here you are!

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

+1
source

I think you can just check your local branch and merge it from a local copy of the origin / a branch.

Sort of:

 git checkout a git merge origin/a 
+1
source

git pull is nothing more than git fetch + git merge . With the --rebase option --rebase you can also reinstall it and not merge it, so you can just switch to another branch and merge / reinstall:

 git checkout a git merge origin/a 

or

 git checkout a git rebase a 

Since it does nothing, I didn’t even use pull at all. I just git fetch regularly so I can easily track changes in other repositories and then git merge/rebase when I'm ready.

0
source

This will check all tracking branches in turn and merge 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 the merge has conflicts, it is rewound with git reset , and you will have to do it manually.

Unverified.

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

0
source

All Articles