41

I've cloned repository A's master branch from git and created my own branch called Li. I've made some changes a while ago and pushed the contents of local Li to remote Li.

Now I've pulled some updates from remote master to my local master branch and from the local master branch to the local Li, and I'm trying to push the updates from local Li to remote Li. However, when I try to run:

git checkout Li
git push origin Li

I get the following error:

error: failed to push some refs to 'git@github.com:anodejs/system.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.

Note that my local master branch is updated (I invoked git pull origin master) and merged into the local Li branch. I did, however, add local Li a new file, so local Li is not identical to local master (but this shouldn't matter, right?)

Thanks, Li

Vlad
  • 18,195
  • 4
  • 41
  • 71
user429400
  • 3,145
  • 12
  • 49
  • 68
  • Did you commit changes on Li branch before trying to push it? – IgorGanapolsky Apr 22 '13 at 17:28
  • 1
    Came to this question with the same problem and the answers here didn't help. Turned out my problem was I wasn't in a branch - see http://stackoverflow.com/a/18601467/5002633 for how I diagnosed / fixed it. – Eborbob Jul 19 '16 at 15:13

4 Answers4

36

Updates were rejected because a pushed branch tip is behind its remote.

git config --global push.default current

Try the command above to set the current branch as default and

git push
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Siva Kandaraj
  • 824
  • 7
  • 15
  • 10
    Note this command changes the fundamental behavior of `git push`, which is often, but not always, intended. Users who want to know what those behaviors are can see [this question](http://stackoverflow.com/questions/13148066/warning-push-default-is-unset-its-implicit-value-is-changing-in-git-2-0) or [this question](http://stackoverflow.com/questions/11872984/what-is-the-difference-between-git-push-default-current-and-push-default-upstrea/11873386#11873386). – Christopher Nov 08 '13 at 03:17
29

Find the diff with git fetch && git log Li..origin/Li. I would guess you've rebased or otherwise recut Li since last time you pushed, but that command should tell you exactly what's in the remote that isn't in the local. You can find what's in either (but not both) with the triple-dot syntax: git log Li...origin/Li.

If the diff is expected, then just merge with git merge origin/Li and then git push. If the change is unwanted, overwrite the remote with git push -f origin Li. Only do this if you want to abandon the remote's changes.

Christopher
  • 42,720
  • 11
  • 81
  • 99
  • 1
    When I invoke the fetch&&log command I can see that there is a merge pull request from origin/master. But when I try "git merge origin/Li" I see that there's nothing to merge (already up-to-date!). I believe that push -f will work, but I think that if I'll figure it out it will help me understand git – user429400 Aug 13 '12 at 14:15
  • gave up, went for push -f which solved the issue as you said. Thanks – user429400 Aug 19 '12 at 20:45
  • Big thanks. I read a few advices on the StackOverflow, but nothing worked for me but this. You made me happy, because I haven't lost my good humour just by problem with git :) It is a little bit frustrating when you are testing solution by solution and still get the same error message ;D Very best – Tomasz Kuter Apr 29 '14 at 15:55
0

You can force push changes to remote address, but it can produce new remote branch

git push subtree_remote_address.git `git subtree split --prefix=subtree_folder`:refs/heads/branch --force
FunkyKat
  • 3,233
  • 1
  • 23
  • 20
0

git branch --set-upstream-to=origin/ master

If you are having the error when you are on 'master' branch, try the following command:

git branch --set-upstream-to=origin/master

Or, a shortcut: git push -u origin master