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

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: How to keep SHAs in commit messages up-to-date after rebase?
  • Git revert/cherry-pick to a commit beyond a merge
  • Make different `pretty` settings for git-log and git-show
  • How does the Bittorrent Sync secret work?
  • Understanding `git reset --hard`
  • A way to automatically sync stable and develop branch in GitHub repo
  • 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?)


    To summarise the answer below, it’s basically “just use origin/master directly, you probably don’t need to actually do this merge”.

  • git http push via WebDAV - what if my username has an “@” in it?
  • git and visual studio 2010 express
  • openshift: can't install lxml for python app
  • How to maintain remote repo (Github, Bitbucket) up to date with the running server
  • Why doesn't the command `gem list` include gems installed with bundler's :git option?
  • Wrong result when using JGit blame
  • 2 Solutions collect form web for “How to pull into not-the-current-branch?”

    You are correct that pull/merge only merges into the current branch.

    You can, however, still use fetch. For instance (names below changed to protect the innocent but the hashes are real):

    $ git branch | grep '^*'
    $ git rev-parse OTHER_BRANCH origin/OTHER_BRANCH
    $ git fetch
    7b9b8e5..1efca56  OTHER_BRANCH -> origin/OTHER_BRANCH
    $ git rev-parse OTHER_BRANCH origin/OTHER_BRANCH

    In this case, fetch updated a bunch of origin/ branches. None of the local branches were updated (git rev-parse output for those remains the same) but the new commits are now in the repo and can be viewed (git log origin/OTHER_BRANCH, gitk --all, etc).

    Depending on your needs, this might be sufficient. In particular you can see what needs to be applied from origin/master onto master, all without leaving your current branch.

    git fetch -u origin master:master

    Merge, update, and pull Git branches without using checkouts

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

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

    Git Baby is a git and github fan, let's start git clone.