How to pull into not-the-current-branch?

Steve Bennett picture Steve Bennett · Mar 17, 2012 · Viewed 9.5k times · Source

Say my current branch is myfeature. I want to get master up to date. Both git merge git pull always merge into the current branch, as far as I can tell.

Is there a way to merge changes from a remote branch (eg, origin/master) into a branch I'm not currently on (master)? I can think of one way:

git stash
git checkout master
git pull origin/master
git checkout myfeature
git stash apply

Is there a better one?

(It's possibly my whole question is wrong: would git fetch automatically update master to match origin/master, if remote-tracking is enabled?)

Answer

pkamb picture pkamb · Aug 10, 2017
git fetch -u origin master:master

Merge, update, and pull Git branches without using checkouts

git fetch -u <remote> <remoteBranch>:<localBranch>

The -u or --update-head-ok ensures that the command still works even if you have the given branch checked out.