How to 'git pull' into a branch that is not the current one?

Straightforward: Updating from a remote branch into a currently not checked-out branch master:

git fetch origin master:master

where origin is your remote and you are currently checked out in some branch e.g. dev.

If you want to update your current branch in addition to the specified branch at one go:

git pull origin master:master

This is answered here: Merge, update, and pull Git branches without using checkouts

# Merge local branch foo into local branch master,
# without having to checkout master first.
# Here `.` means to use the local repository as the "remote":
git fetch . foo:master

# Merge remote branch origin/foo into local branch foo,
# without having to checkout foo first:
git fetch origin foo:foo

As it turns out, the answer is deceptively simple:

$ git fetch                           # Update without changing any files
$ git branch -d master                # Remove out-of-date 'master' branch
$ git checkout --track origin/master  # Create and check out up-to-date 'master' branch

This allows you to update the master branch without switching to it until after it has been updated.


You're worried about something that cannot be fixed, as Git operations are not atomic. You will always have a hole where your working directory is half way between branches, even if you update master without first switching to it. This is why Git is not a deployment tool.

Since you're not actually committing code in your production environment (I hope), you don't actually need to have a branch checked out. You can simply do a git fetch to update your remote refs, and then git checkout origin/master to move the working directory directly to the commit currently pointed to by origin/master. This will put you in a detached head state, but again, as you're not committing code, this doesn't matter.

This is the smallest hole you're going to get, but as I said, a hole still exists; checkout is not atomic.


You've got a worktree you don't want to touch, so use another one. Clone is cheap, it's built for this.

git fetch origin master       # nice linear tree
git clone . ../wip -b master  # wip's `origin/master` is my `master`
cd ../wip                     # .
git pull origin origin/master # merge origin's origin/master
git push origin master        # job's done, turn it in.
cd ../main
rm -rf ../wip                 # wip was pushed here, wip's done

git checkout master           # payload

The problem with all the other answers here is, they don't actually do the pull. If you need the merge or rebase you've got pull set up for, you need another worktree and the above procedure. Otherwise just git fetch; git checkout -B master origin/master will do.