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
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,
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:
- How to find whether a file is present in multiple branches in git
- Git: Merge a Remote branch locally
- Not Possible to switch branch after --skip-worktree
- How to list only active / recently changed branches in git?
- what are “automerged” files in git, and how do you list them all?
- Cannot push new branch to git server
$ 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.
7 Solutions collect form web for “How to 'git pull' into a branch that is not the current one?”
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
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
Straight forward: Pulling from a remote branch into a currently not checked-out branch master:
git pull origin master:master
where origin is your remote and you are currently checked out in some branch e.g. dev
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 can use update-ref for this:
git fetch git update-ref refs/heads/master origin/master git checkout master
Note that this would throw away any local commits in the master branch. In your case there won’t be any so this is okay. For other people trying to do this where there are local commits, I don’t think it’s possible, since merge can only be run on the current branch.
Doesn’t this do it (assuming you are in your feature branch):
git pull origin master
This’ll pull the latest master into your current feature branch.