Malvineous Malvineous - 2 months ago 9
Git Question

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

When you run

git pull
on the
master
branch, it typically pulls from
origin/master
. I am in a different branch called
newbranch
, but I need to run a command that does a
git pull
from
origin/master
into
master
but I cannot run
git checkout
to change the selected branch until after the pull is complete. Is there a way to do this?

To give some background, the repository stores a website. I have made some changes in
newbranch
and deployed them by switching the website to
newbranch
. Now those changes have been merged upstream into the
master
branch, I am trying to switch the website back to the
master
branch as well. At this point,
newbranch
and
origin/master
are identical, but
master
is lagging behind
origin/master
and needs to be updated. The problem is, if I do it the traditional way:

$ git checkout master
# Uh oh, production website has now reverted back to old version in master
$ git pull
# Website is now up to date again


I need to achieve the same as above (
git checkout master && git pull
), but without changing the working directory to an earlier revision during the process.

Answer

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