git pull –rebase upstream & git push origin rejects non-fast-forward?

I’ve implemented classic OSS maintainer/contributor git workflow for a company project on github, however one edge case produces some weird results that I’m not sure how to get around.

Lets say there is a typical project that I forked and added upstream remote to keep it up to date.

  • What to do when new commits happen after a git rebase?
  • How can I set up an editor to work with Git on Windows?
  • git, how to reference last commit in current branch
  • git push using PuTTY on Windows fails (fatal: The remote end hung up unexpectedly)
  • Jenkins Pipeline: is it possible to avoid multiple checkout?
  • Set git commit hash as dll version number in visual studio
  • git clone<project>.git
    git remote add upstream<company>/<project>.git

    For the purposes of this example this fork is behind by a few commits.

    git reset --hard HEAD~5 && git push --force

    I work on this fork and push some commits, before pushing my last commit and creating a pull request I update my fork’s clone to make sure there are no conflicts.

    touch foo && git add foo && git commit -m foo && git push
    touch bar && git add bar && git commit -m bar
    git pull --rebase upstream master
     * branch            master     -> FETCH_HEAD
    First, rewinding head to replay your work on top of it...
    Applying: foo
    Applying: bar

    Now, when I try to push to my fork I get rejected.

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

    What should I do next? All I want is for the pull request to contain foo and bar commits, however…

    When I pull, pull request contains duplicate foo commits as well as extra merge one.

    git pull
    Merge made by the 'recursive' strategy.
    git push

    On github pull request looks like this.

    Showing 4 unique commits by 1 author.
    kozhevnikov  foo    4 minutes ago
    kozhevnikov  foo    4 minutes ago
    kozhevnikov  bar    2 minutes ago
    kozhevnikov  Merge branch 'master' of<project>    just now

    When I git pull --rebase instead of pull, at best it’ll include other people’s commits into my pull request (those from reset), and at worst it gives me merge conflicts.

    When I git push --force without any pull or --rebase it works perfectly, however I’m very uneasy in saying to everyone use the force or making it part of standard workflow as I can imagine few people or a small subteam collaborating on a single fork and stepping on each other’s toes with forced push.

    Any ideas? What am I missing?

  • View git diff from individual parents
  • Git, rewriting history of master branch and associated tags
  • How does one `git log` tagged commits only?
  • how to set magit commit author?
  • Can't resolve rebase conflict
  • PS1 env variable does not work on mac
  • One Solution collect form web for “git pull –rebase upstream & git push origin rejects non-fast-forward?”

    When you

    git pull --rebase upstream master

    you are rewriting your own history, since you are rebasing your master branch on the updated upstream repository. When you push your rebased repo to your fork git complains. You need to push with –force

    git push --force origin master
    Git Baby is a git and github fan, let's start git clone.