schmmd
schmmd

Reputation: 19468

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 [email protected]:asdf.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]: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.

Upvotes: 2

Views: 1889

Answers (2)

lbonn
lbonn

Reputation: 2599

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

Upvotes: 4

Simon Boudrias
Simon Boudrias

Reputation: 44609

When you checkout -b, you finish on the new branch. So you reset and merge onto backup already. Try it this way:

$ git branch backup
$ git fetch origin master
$ git reset origin/master --hard
$ git merge backup
$ git push origin master

If you're on Windows, look out posh-git to get visual feedback of your state on your command line (with powershell).

If you're on Mac or Linux, then checkout some dot-files repo on github to get similar functionnality.

Upvotes: 1

Related Questions