7

I am a Mercurial user, and I am confused about the behaviour of remote branches. I have a remote branch origin/dev, and I want to replicate it on a local branche dev. What I would like is that:

  • whenever I git pull, changes to origin/dev are merged into dev
  • whenever I git push, changes to dev are merged into origin/dev

So I created a tracking branch with

git branch --track dev origin/dev

which, to the best of my knowledge, should do exactly what I need.

Still, I was working on a feature branch and issued a git pull. When I later issued git checkout dev I received the puzzling message

Your branch is behind 'origin/master_dev' by 2 commits, and can be fast-forwarded.

So it seems that my local branch was not updated after all. Is there a way to have the branch updated to the remote one whenever I pull and I am not currently in that branch? If not, am I correct that git merge (without any arguments) on branch dev is enough to restore the situation?

Andrea
  • 19,377
  • 21
  • 107
  • 177
  • Related: [How do you make an existing Git branch track a remote branch?](http://stackoverflow.com/q/520650/456814). –  May 23 '14 at 18:22

1 Answers1

5

The command git pull fetches updates from all remote branches (i.e, updates all the remote tracking branches). But merges only the current branch. This is a default behavior of git pull when no argument passed.

As you were on a diff branch when you git pull, it just updated the remote tracking branch for dev. Now git merge would be enough to update your local branch dev.

Karthik Bose
  • 26,768
  • 3
  • 30
  • 43
  • 3
    As a side note, [fetch and merge, don't pull](http://longair.net/blog/2009/04/16/git-fetch-and-merge/). – Shahbaz Jul 26 '12 at 09:58
  • Acutally I just tried to simply merge and the result is `fatal: No commit specified and merge.defaultToUpstream not set.` even if the branch was created as tracking – Andrea Jul 26 '12 at 10:34
  • 1
    OK. To get it work, you can do this `git merge remote_name/branch_name`. In your case, `git merge origin/master_dev`. – Karthik Bose Jul 26 '12 at 10:53
  • 15
    Even though the branch is created as tracking, you need to set `merge.defaultToUpstream` as true. It tells GIT to merge with tracking branch when no arguments passed. So either do `git config merge.defaultToUpstream true` and `git merge` OR `git merge remote/branch` – Karthik Bose Jul 26 '12 at 11:02