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,376
  • 2
  • 14
  • 31

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,127
  • 1
  • 14
  • 15
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
  • 30,802
  • 16
  • 100
  • 180