Search code examples
gitgithubrebase

Pushing to remote fails because "tip of your current branch is behind its remote counterpart"


terminal commands and output duplicated below, showing git pull, rebase and push ultimately failing to to the tip of the branch being behind

$ git config pull.ff only
$ git pull
Already up to date
$ git checkout EditReadMe1
Switched to branch 'EditReadMe2'
$ git rebase master
Current branch EditReadMe2 is up to date
$ git push myremote EditReadMe2
To https://github.com/[redacted repo]-playground
 ! [rejected]         EditReadMe2 -> EditReadMe2 (non-fast-forward)
error: failed to push some refs to 'https://github.com/[redacted repo]-playground'
hint: Updates were rejected because the tip of your current branch is behind
hint: it's remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details

My friend is trying to help me learn to resolve conflicts in pull requests. He created a repository. I cloned it, and created a branch called "EditReadMe2". I pushed "EditReadMe2" to the repository, and he created a conflict.

I originally ran the following commands

git checkout master
git pull
git checkout EditReadMe2
git rebase master

It warned me of a conflict which I resolved, but when I tried pushing EditReadMe2 it gave me the error.

I entered in the commands again to show my terminal in the attached image, because I don't know how its possible for the branch to be behind when I pull and rebase a second time, and it tells me everything is up to date, but then it still fails.

Force pushing solved the problem, but I want to know how to do this without using --force.


Solution

  • The command sequence is incomplete.
    After git checkout EditReadMe2 you need to perform another git pull.

    git pull updates the working index of the current branch, not of all local branches you have.
    When you issue the rebase command, you are rebasing an updated master into your "old" EditReadMe2 branch.

    Anyway the git rebase is fine to be used in such way.


    An hint:
    if you are switching to master, git pulling, switching back to EditReadMe2 only for the purpouse of rebasing, you can use the following sequence and save a couple of commands:

    Assuming you are in EditReadMe2

    git pull
    git rebase origin/master