0

What's the difference between:

(master) ~ $ git merge foo

and

(master) ~ $ git merge origin/foo

When I did the first one, it told me "already up-to-date", with the second one, I got a bunch of updates and comment to save in my default editor (vim). Like if it was two different branches. But if I do git branch -r, I have:

  origin/dev
  origin/foo
  origin/master

If I do git branch, I have:

* dev
  foo
  master

I'm a bit confused with the origin/ prefix.

DevonDahon
  • 4,134
  • 2
  • 37
  • 57
  • `origin` is the common name of the remote repo, you can have branches in local or remote repos. – Ivan Dec 08 '20 at 10:37
  • foo is local where you can work, origin/foo is the remote tracking branch (tracking foo on origin). – matt Dec 08 '20 at 10:37
  • `origin/foo` - usually it is remote branch; and `foo` - is local branch. – kosist Dec 08 '20 at 10:37
  • 2
    Does this answer your question? [git merge origin/branch vs. merge branch locally](https://stackoverflow.com/questions/36282085/git-merge-origin-branch-vs-merge-branch-locally) – NanoBit Dec 08 '20 at 10:38
  • It's worth mentioning that all `git merge` operations work with a *commit* (as found by its hash ID). Try running `git rev-parse foo` and then `git rev-parse origin/foo`; this may provide enlightening. – torek Dec 08 '20 at 10:43

3 Answers3

1

When you do git merge foo, you try to merge with your local branch, so it's already up-to-date.

When you do git merge origin/foo, you try to merge from remote branch.

NRE
  • 399
  • 1
  • 7
1

The command:

git merge foo

says to merge into the currently checked out branch the local branch called foo. By "local," I mean the local version of the foo branch on which you might have been doing your work.

The command:

git merge origin/foo

says to merge into the currently checked out branch the remote tracking branch called origin/foo. foo and origin/foo might be pointing to the same commit, or they might not. For example, if you have recently fetched, then it's possible (or even likely) that origin/foo would be more recent than the local foo you have on your machine.

By the way, if you're wondering what the utility of the remote tracking branches is, these branches are local branches on your machine which exist as proxies for the true remote branches in the repository. Git operations primarily operate locally, and this distributed support is one reason which makes Git powerful.

For added information, suppose you wanted to push your local foo branch to the remote, via:

git push origin foo

In this case, origin actually refers to the remote Git repository (e.g. something like GitHub or Bitbucket), and foo refers to the local branch you want to push.

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
0

git merge origin/foo will merge remote branch into your current local active branch. Usual workflow is that one merges local branches into local branches. So do the following:

git checkout foo - it will switch to local foo branch;

git pull origin - if foo branch tracks remote origin/foo branch - it will pull all remote updates to local branch;

git checkout master - switch to local master;

git merge foo - finally you will merge foo branch into master.

kosist
  • 1,784
  • 2
  • 13
  • 23