3

Assume that my local repository is one commit behind the repository at github.

Then I commit one commit at the local repository

At this time

A------>commit 1 Github/master

A------>commit 2 local repository/master

I do the following steps to push commit 2 to github:

  1. git fetch origin master
  2. git rebase origin/master
  3. git push origin master

But I got the following errors:

enter image description here If I try to replace step 1 with git fetch origin, it works well

Then I tried git fetch origin master:tmp, a branch named tmp successfully created

So, My question is

why git fetch origin master sometimes works(in the case git fetch origin master:tmp), while sometimes not work in the case step 1?

Charles0429
  • 1,406
  • 5
  • 15
  • 31
  • 1
    Are you sure that `git rebase origin/master` was successful? It seems it wasn't. Maybe you could show the output of that. – janos Dec 31 '13 at 13:57
  • What version of Git are you using? – CB Bailey Dec 31 '13 at 14:47
  • Almost, but not quite, a duplicate of [this](http://stackoverflow.com/questions/1741143/git-pull-origin-mybranch-leaves-local-mybranch-n-commits-ahead-of-origin-why/1743870#1743870). I'm guessing this is Git <1.8.4. – CB Bailey Dec 31 '13 at 14:49
  • @Charles0429 please show the output of `git status` too – janos Dec 31 '13 at 15:02

2 Answers2

8

This isn't about working or not, but about where you're asking git store what it downloads. If you omit the target in a refpec, you're asking git to store it in FETCH_HEAD. Thus, git fetch origin master is really git fetch origin master:FETCH_HEAD, and you're not touching origin/master or any ref at all (as you can see from the output, master -> FETCH_HEAD).

When you run git fetch origin master:tmp you're asking it to download the master branch (this is yet another layer, guessing that you want to deal with branches) and store it in a local branch named tmp. You would also see this mapping in the output.

If you want to update the remote-tracking branches, simply call git fetch origin. Calling the two-argument version of git-fetch is rarely something you want to do.

Carlos Martín Nieto
  • 5,207
  • 1
  • 15
  • 16
3

Instead of

git fetch origin master
git rebase origin/master

you should just do

git pull --rebase

That will do the right thing.

Robin Green
  • 32,079
  • 16
  • 104
  • 187