3

Github is the default repository for my project (just "origin" renamed to "github"). Something has happened so that "git push" causes a "non-fast-forward updates" error, even though "git push github master" works. "git pull" and "git pull github master" both indicate an up-to-date status. How can I (a) be sure there are no unmerged changes on Github and (b) correct the non-fast-forward error?

$ git status
# On branch master
nothing to commit (working directory clean)
$ git pull
Already up-to-date.
$ git pull github master
From github.com:MikeBlyth/mission_net
 * branch            master     -> FETCH_HEAD
Already up-to-date.
$ git push github master
Everything up-to-date
$ git push
To [email protected]:MikeBlyth/mission_net.git
 ! [rejected]        add_command -> add_command (non-fast-forward)
error: failed to push some refs to '[email protected]:MikeBlyth/mission_net.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

My git config file is

[core]
  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true
[remote "github"]
  url = [email protected]:MikeBlyth/mission_net.git
  fetch = +refs/heads/*:refs/remotes/github/*
[branch "master"]
  remote = github
  merge = refs/heads/master
[remote "heroku"]
  url = [email protected]:joslink.git
  fetch = +refs/heads/*:refs/remotes/heroku/*
  merge = refs/heads/master
[remote "heroku"]
url = [email protected]:joslink.git
fetch = +refs/heads/*:refs/remotes/heroku/*
2
  • I have changed my initial answer.
    – VonC
    Commented Jan 12, 2013 at 9:40
  • To summarize the answers of michas and VonC, the problem was that "git push" by default tries to push all the branches, and I had a branch (add_command) that was out of sync.
    – Mike Blyth
    Commented Jan 12, 2013 at 11:03

2 Answers 2

2

The syntax of 'git push' supports both an explicit and a shorthand version. The explicit version git push github master works for you. The shorthand version git push does not.

If you use the shorthand version you do not tell git what remote to use and what local branch shall be pushed to what remote branch. Hence git has to guess what you mean.

You can configure this with the setup of your remote and the push.default config:

   push.default
       Defines the action git push should take if no refspec is given on
       the command line, no refspec is configured in the remote, and no
       refspec is implied by any of the options given on the command line.
       Possible values are:

       ·    nothing - do not push anything.

       ·    matching - push all matching branches. All branches having the
           same name in both ends are considered to be matching. This is
           the default.

       ·    upstream - push the current branch to its upstream branch.

       ·    tracking - deprecated synonym for upstream.

       ·    current - push the current branch to a branch of the same
           name.

Have a look at git branch -vv to check what branch is tracked by your current branch. Then check git config --get push.default to verify it is doing what you expect.

2

The explanation could be related to the default refspec used for remote "github":

+refs/heads/*:refs/remotes/github/*

A simple git push would push:

  • to the remote associated to master (here "github"), because master is the current branch (according to the git status)
  • all the other branches (master and add_command branches)

add_command is the one which is not in sync with github remote.

git checkout add_command 
git pull github

Then a git push would work.

5
  • Wouldn't a git checkout master be enough to leave detached head state? then he can continue to pull and push.
    – dunni
    Commented Jan 12, 2013 at 9:26
  • @dunni but he already is in master (according to his status). At this point, a reset --hard is the surest way to make sure there won't be any "non-fast-foward" state between local and remote "github".
    – VonC
    Commented Jan 12, 2013 at 9:27
  • @dunni actually, I don't think a "detached head" was the root cause. I have changed my answer.
    – VonC
    Commented Jan 12, 2013 at 9:39
  • Thanks for the concise answer. I used both yours and michas' (since I didn't understand the bit about why a simple push pushes all the branches) to fix the problem.
    – Mike Blyth
    Commented Jan 12, 2013 at 10:58
  • This worked for me. I did a checkout of the branch (even though I was already on the branch). Then git pull origin [branchname].
    – eeejay
    Commented Mar 30, 2013 at 21:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.