87

Scenario:

  1. I make a new branch
  2. hack on it
  3. commit it
  4. push it
  5. hack on it some more
  6. commit again
  7. try to push again

Git responds:

Updates were rejected because the tip of your current branch is behind its remote counterpart. etc.

I'm the only one hacking on this branch - no one else is touching it. The remote branch is actually behind the local branch. I shouldn't have to pull at all.

(And if I do pull, Git reports conflicts between the two, and forces me to merge the branch into itself)

Why is this (likely) happening? And how can I diagnose/fix it?

To be clear, I'm not branching anywhere, and no one else is working on it:

Remote: Commit A -------- Commit B  

Local:  Commit A -------- Commit B -------- Commit C  

C is a straight continuation of B, no branching involved. But git thinks C is a branch of A:

Remote: Commit A -------- Commit B  

                  ------- Commit C  
                /  
Local:  Commit A -------- Commit B  

It's not; it's a straight continuation of B.

1
  • 1
    The output of git remote -v and git show remote origin (assuming origin is the remote you are having trouble with) may be helpful
    – Ben Graham
    Commented Sep 29, 2012 at 6:04

4 Answers 4

201

You probably did some history rewriting? Your local branch diverged from the one on the server. Run this command to get a better understanding of what happened:

gitk HEAD @{u}

I would strongly recommend you try to understand where this error is coming from. To fix it, simply run:

git push -f

The -f makes this a “forced push” and overwrites the branch on the server. That is very dangerous when you are working in team. But since you are on your own and sure that your local state is correct this should be fine. You risk losing commit history if that is not the case.

5
  • 13
    That was it. At step 2, I did an "Amend Last Commit", then pushed, then hacked some more, then tried to push again. I misunderstood the way Amend works. Thanks!
    – Tim Janke
    Commented Oct 2, 2012 at 0:10
  • 4
    This seems really useful - but could someone explain the 'HEAD @{u}' syntax?
    – ChrisV
    Commented Dec 6, 2013 at 11:46
  • 5
    Both the HEAD and the @{u} refer to commits. They tell gitk, which branches to display. HEAD refers to the currently checked out branch, @{u} is short for HEAD@{u}, which represents the upstream branch of the currently checked out branch. So for eg. master, that is usually origin/master.
    – Chronial
    Commented Dec 6, 2013 at 12:59
  • Had the same scenario - had to do a rescan and merge conflicts. Using gitk helped a lot!
    – brichins
    Commented Jul 6, 2016 at 21:52
  • Happens to me if I make many simple commits locally (not ammend), and try to push. I know nobody else is changing things on github. Gitk really showed like the remote was different than the local, despite it is expected, it should show the local as a newer version than the remote and not the remote as a fork (looked like that, I dont know much gitk to tell) Commented Sep 20, 2016 at 1:57
6

The solution is very simple and worked for me.

Try this :

git pull --rebase <url>

then

git push -u origin master
4

This happened to me when I was trying to push the develop branch (I am using git flow). Someone had push updates to master. to fix it I did:

git co master
git pull

Which fetched those changes. Then,

git co develop
git pull

Which didn't do anything. I think the develop branch already pushed despite the error message. Everything is up to date now and no errors.

0

To diagnose it, follow this answer.

But to fix it, knowing you are the only one changing it, do:
1 - backup your project (I did only the files on git, ./src folder)
2 - git pull
3 - restore you backup over the many "messed" files (with merge indicators)

I tried git pull -s recursive -X ours but didnt work the way I wanted, it could be an option tho, but backup first!!!

Make sure the differences/changes (at git gui) are none. This is my case, there is nothing to merge at all, but github keeps saying I should merge...

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