Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Can you git pull a specific branch?

You can clone a specific branch from a Git repository using the git clone –single-branch –branch command. This command retrieves all the files and metadata associated with one branch. To retrieve other branches, you'll need to fetch them later on.

How do I pull a branch from another branch?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch.

Does git pull pull from current branch?

A git pull is what you would do to bring a local branch up-to-date with its remote version, while also updating your other remote-tracking branches. In other words: git pull will always merge into the current branch. So you select which branch you want to pull from, and it pulls it into the current branch.


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.


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.