schmmd schmmd - 17 days ago 7
Git Question

Can't push in GIT, but rebase up-to-date

Can anyone explain the state of my repository? I can't push because there are changes on the server I don't have, but I can't rebase because git tells me there are no new changes.

$ git branch
* master

$ git push origin master
To git@github.com:asdf.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:asdf.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git fetch origin master
From github.com:knowitall/nlptools
* branch master -> FETCH_HEAD

$ git rebase origin master
Already on 'master'
Your branch is ahead of 'origin/master' by 3 commits.
Current branch master is up to date.

$ git pull origin master


The merge that it proposes is empty.

Merge branch 'master' of github.com:knowitall/nlptools

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.


If I create a branch, reset with origin/master, and then merge the branch, I still can't push.

$ git checkout -b backup
$ git checkout master
$ git fetch origin master
$ git reset origin/master --hard
$ git merge backup
$ git push origin master
! [rejected] master -> master (non-fast-forward)


Now if I
reset
and
pull
, I see a new commit. Why didn't
fetch origin master
find this new commit in the first place? How can I make sure my repositories representation of origin is up to date? It seems that I need to have a successful pull in order to have the origin up to date.

Answer

The problem seems to lie in improper git fetch usage: git fetch origin master reads master as a refspec and does not behave like a regular fetch. More specifically, it just makes FETCH_HEAD to point on the remote master.

When fetch is used without refspec, it uses +refs/heads/*:refs/remotes/origin/* as a default which updates all the refs origin/*.

Try git fetch origin or git fetch instead.

Here is a good doc about it for more details: https://git-scm.com/book/th/ch9-5.html

Comments