35

As a new git user, who is an expert with SVN and CVS, I am struggling to get the most basic of git functions to work.

I'm using a shared repo at assembla.com

I created a local clone, and added a file:

$ git clone repository-url
$ echo "hello" > ha.txt
$ git add -A
$ git commit -a -m "haha"
$ git push

NOTE: at this point I got "No refs in common and none specified; doing nothing" error. After some hours googling, I found the solution was to type this

$ git push origin master

Then I went onto another computer, modified the file, and commit-ed it (surprisingly, I didn't need to do the git push origin magic). Then I back to the main computer, modified it again, so I could see how merge works.

$ git fetch
$ git merge

Now I get the error:

fatal: No commit specified and merge.defaultToUpstream not set.

Looking at the man page for "git merge", you have to specify something like this:

$ git merge [< commit >..]

The problem is, I cant find out what < commit > means, and what it should be. E.g. should it be a file, a repo, a message, a version?

I have not created a branch - I'm just working on the "head" or master as I think git calls it

Unfortunately, google is not much help on this one. The man pages seem to expect you to know what a < refspec >, < commit > and origin are.

Any help on this noob problem appreciated.

janos
  • 120,954
  • 29
  • 226
  • 236
John Little
  • 10,707
  • 19
  • 86
  • 158
  • Note: soon, `git config merge.defaultToUpstream true` won't be needed anymore by default for tracking branches: see [my answer below](http://stackoverflow.com/a/24716957/6309) – VonC Jul 12 '14 at 20:19

4 Answers4

44

Usually you do not invoke git merge without arguments (at least I don't know anyone who does). If you want that merge defaults to the tracking branch, you need to set merge.defaultToUpstream to true: git config merge.defaultToUpstream true. Your master branch has to track origin/master in this case: git branch --set-upstream master origin/master. This is done automagically if origin/master was already present when you cloned.

Personally, I do git fetch and then git merge origin/master or git pull if I have no local commits.

Edit: As VonC mentioned merge.defaultToUpstream defaults to true since Git 2.0.

Community
  • 1
  • 1
atamanroman
  • 11,607
  • 7
  • 57
  • 81
  • Good info, much appreciated. The command: "git branch -u origin/master master" gives error that u is an unknown switch, unfortunatenly. I tried cloning the repo again, get same merge problem. its a real hello world example. – John Little Apr 25 '13 at 20:29
  • I tried "git merge origin master". It worked. Do I have to do this every time? Are origin and master short cuts for the local and remote repo URLs? – John Little Apr 25 '13 at 20:36
  • Seems that tracking is not enough. Have a look here: http://stackoverflow.com/questions/11666286/git-tracking-remote-branches I have never set this flag in two years. – atamanroman Apr 25 '13 at 20:39
  • I dont understand any of http://stackoverflow.com/questions/11666286/git-tracking-remote-branches but setting git config merge.defaultToUpstream true seems to have fixed the problem for good. – John Little Apr 25 '13 at 21:53
8

Note: if your master branch is already tracking origin/master (you can see that with git branch -avvv, or a longer alias), then a git merge will not display anymore:

fatal: No commit specified and merge.defaultToUpstream not set.

The next Git 2.à.x (Q3 2014) will remove that error message:
see commit a01f7f2 by Felipe Contreras (felipec):

merge: enable defaulttoupstream by default

There's no point in this:

% git merge
fatal: No commit specified and merge.defaultToUpstream not set.

We know the most likely scenario is that the user wants to merge the upstream, and if not, he can set merge.defaultToUpstream to false.

That means you won't need anymore to do a:

git config merge.defaultToUpstream true

As noted with Git 2.33 (Q3 2021): Defaults to true.

See commit 8603c41 (07 Jun 2021) by Felipe Contreras (felipec).
(Merged by Junio C Hamano -- gitster -- in commit 11fac26, 08 Jul 2021)
Signed-off-by: Felipe Contreras

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
5

Read Pro Git, it's a very good way to get to know Git.

It will tell you that your merge, done with no branch created and with no branch set as merge source, makes no sense.

It will also tell you the difference between git fetch and git pull which is important.

Fetching changes does not move your branch, but pulling does.

HonkyTonk
  • 1,961
  • 11
  • 11
0

Hope this is a much needed settings after the initial git setup. Set the default branch mapping as a global option,

git config --global merge.defaultToUpstream true

sajin tm
  • 323
  • 3
  • 9