How to pull into not-the-current-branch?
Solution 1:
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, which otherwise gives the error:
fatal: Refusing to fetch into current branch refs/heads/master of non-bare repository
Solution 2:
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 '^*'
* SOMEBRANCH
$ git rev-parse OTHER_BRANCH origin/OTHER_BRANCH
7b9b8e57cf19964b60ebda0f03a1d5da3de9e2fe
7b9b8e57cf19964b60ebda0f03a1d5da3de9e2fe
$ git fetch
7b9b8e5..1efca56 OTHER_BRANCH -> origin/OTHER_BRANCH
$ git rev-parse OTHER_BRANCH origin/OTHER_BRANCH
7b9b8e57cf19964b60ebda0f03a1d5da3de9e2fe
1efca56c08b7a0f511a3951195656a798c56aa62
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.