75

Say my current branch is myfeature. I want to get master up to date. Both git merge git pull always merge into the current branch, as far as I can tell.

Is there a way to merge changes from a remote branch (eg, origin/master) into a branch I'm not currently on (master)? I can think of one way:

git stash
git checkout master
git pull origin/master
git checkout myfeature
git stash apply

Is there a better one?

(It's possibly my whole question is wrong: would git fetch automatically update master to match origin/master, if remote-tracking is enabled?)

2

3 Answers 3

93
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

Sign up to request clarification or add additional context in comments.

2 Comments

Very nice! – saves the nuisance of 'switch to main, pull, switch to branch' before rebasing my stuff to the current main...
Note that for the OP's question (non-working branch), -u is not needed, and should generally be avoided (unless you know exactly why you need it). Also, to accept non-fast-forward updates (e.g. rewind/rebase), you need to add + to your refspec, i.e. +<remoteBranch>:<localBranch> (assuming these refs are in refs/heads). See Note at: git-scm.com/docs/git-fetch#Documentation/…
5

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.

2 Comments

Ok, I see what you're saying - I can start using commands like "git branch --merged origin/master" instead of "git branch --merged master". But otherwise, just a minor annoying limitation of git.
You can also automate the whole "stash, checkout other branch, pull, return to previous branch, stash apply" sequence. To find what branch you're on now: $ if name=$(git symbolic-ref -q HEAD); then ... followed by a check for $name having the form refs/heads/* (and stripping off the refs/heads/ part). See also stackoverflow.com/questions/9752619/get-pretty-git-rev-name. Watch out for a failed merge though!
2

I've begun using github's hub sync helper to automate this process, rather than relying on fetching individual branches that change between the various repos you're working on:

hub is an extension to command-line git that helps you do everyday GitHub tasks without ever leaving the terminal. https://hub.github.com/

$ hub sync
  • If the local branch is outdated, fast-forward it;
  • If the local branch contains unpushed work, warn about it;
  • If the branch seems merged and its upstream branch was deleted, delete it.

Running hub sync safely updates all of your local branches to the version on the remote.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.