2

This is not a question on git merge origin branch vs git merge origin/branch

git merge <branch> merges the into the working branch. And so we need to specify the "source" of the branch. It can be a locally available origin/<branch> or the local working branch heads/branch.

But what's the need for origin? Unlike git pull we don't need to specify which remote source to fetch the from. A merge operation, to my understanding, is a local operation.

clamentjohn
  • 3,417
  • 2
  • 18
  • 42
  • 1
    Note that, syntactically, `git merge origin branch` is like writing `git merge A B`. *Both arguments are treated as names.* The name `origin` *means* `origin/HEAD` as you can see in [the gitrevisions documentation](https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html), so this is equivalent to `git merge origin/HEAD branch`. That calls for an *octopus merge* unless `origin/HEAD` is already merged or maps to an ancestor of `branch`. (The TL;DR version of all of this is ***never run `git merge origin branch`*** as it means something you *don't want*.) – torek Aug 01 '18 at 14:51

1 Answers1

1

The git pull command lets you specify a default remote branch to be used for fetching, as well as the default target local branch as the merge target. It makes sense to do this, because typically a given local branch will only have one upstream on the remote.

On the other hand, the git merge operation could be happening with any source branch, with your local branch as the target. Therefore, it makes less sense to allow for defining a default source branch to be used in merging.

The one instance where a default source branch for merging would make sense is of course git pull, when you want to update your local version of the remote branch. But, this merge scenario is actually part of git pull, and usually is not done a separate merge.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Yes, `git merge` can happen from any source. But specifying `git merge origin master` doesn't download from origin (`git fetch -all`). So isn't the `origin` argument useless? – clamentjohn Aug 01 '18 at 06:16
  • @ClamentJohn `git merge origin master` is not valid Git syntax, AFAIK. When you merge in Git locally, you always do it with _local_ branches. Keep in mind that even `origin/master` is still a _local_ branch, which just happens to track the actual branch which is on the remote. – Tim Biegeleisen Aug 01 '18 at 06:26
  • Thank you. I was confused. So, `git merge` is a local operation and `git merge origin master` isn't a valid operation. Or git just ignores the users ignorance! – clamentjohn Aug 01 '18 at 06:30
  • Yeah...I was sort of wondering whether that was even running for you. I've never done a merge using that syntax, nor do you have to. – Tim Biegeleisen Aug 01 '18 at 06:31
  • I havn't. I was just making sure for a question I answered on SO https://stackoverflow.com/questions/51625133/git-merge-origin-master-doesnt-work – clamentjohn Aug 01 '18 at 06:32