Git push rejected "non-fast-forward"

356,001

Solution 1

It looks, that someone pushed new commits between your last git fetch and git push. In this case you need to repeat your steps and rebase my_feature_branch one more time.

git fetch
git rebase feature/my_feature_branch
git push origin feature/my_feature_branch

After the git fetch I recommend to examine situation with gitk --all.

Solution 2

Probably you did not fetch the remote changes before the rebase or someone pushed new changes (while you were rebasing and trying to push). Try these steps:

#fetching remote 'feature/my_feature_branch' branch to the 'tmp' local branch 
git fetch origin feature/my_feature_branch:tmp

#rebasing on local 'tmp' branch
git rebase tmp

#pushing local changes to the remote
git push origin HEAD:feature/my_feature_branch

#removing temporary created 'tmp' branch
git branch -D tmp

Solution 3

try this command

$ git push -f -u origin <name of branch>

i.e $ git push -f -u origin master

Solution 4

I had this problem! I tried: git fetch + git merge, but dont resolved! I tried: git pull, and also dont resolved

Then I tried this and resolved my problem (is similar of answer of Engineer):

git fetch origin master:tmp
git rebase tmp
git push origin HEAD:master
git branch -D tmp

Solution 5

I'm late to the party but I found some useful instructions in github help page and wanted to share them here.

Sometimes, Git can't make your change to a remote repository without losing commits. When this happens, your push is refused.

If another person has pushed to the same branch as you, Git won't be able to push your changes:

$ git push origin master
To https://github.com/USERNAME/REPOSITORY.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/USERNAME/REPOSITORY.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.

You can fix this by fetching and merging the changes made on the remote branch with the changes that you have made locally:

$ git fetch origin
# Fetches updates made to an online repository
$ git merge origin YOUR_BRANCH_NAME
# Merges updates made online with your local work

Or, you can simply use git pull to perform both commands at once:

$ git pull origin YOUR_BRANCH_NAME
# Grabs online updates and merges them with your local work
Share:
356,001
Frankline
Author by

Frankline

About me I'm a developer in various languages, but most notably Java/J2EE and Python/Django, shifting more and more in the Python/Django direction every day. I also enjoy playing around with PHP and Ruby, but I don't pretend to be an expert. Of late I've been more and more interested in Blockchain technology and Machine Learning.

Updated on July 08, 2022

Comments

  • Frankline
    Frankline almost 2 years

    I am fairly new to git, yet currently using it to manage our code in a team environment. I had some rebasing issues, and I fixed them using:

    git checkout --ours filename.txt
    git add filename.txt
    git rebase --continue
    

    Now I wish to push my changes, and so running the following command:

    $ git push origin feature/my_feature_branch
    

    Gave me the following error:

    To ssh://[email protected]:7999/repo/myproject.git
     ! [rejected]        feature/my_feature_branch -> feature/my_feature_branch (non-fast-forward)
    error: failed to push some refs to 'ssh://[email protected]:7999/repo/myproject.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.
    

    What can I do to get rid of this error?

    Note: I am avoiding the use of --force option as much as possible.

  • Jing Li
    Jing Li over 8 years
    This solves my problem: when I committed my code, I did a rebase (too late, there were already changes, should do it before commit). Then even there was no conflict, I could not push. After applying above magic, it worked. Thanks.
  • Mike Q
    Mike Q about 7 years
    This screwed me, I pushed stuff directly to master and pushed the deploy from an entire day... and everyone is pissed.... AWESOME TIPS!
  • Mirv - Matt
    Mirv - Matt almost 7 years
    Probably want to explain what you're doing in summary before giving someone a dangerous tool.
  • GChuf
    GChuf over 4 years
    Helped me when I received the error in question while pulling a remote branch.
  • gcr
    gcr almost 4 years
    That worked for my case when the others didn't. Sometimes you just have to tell git -f -u
  • mathtick
    mathtick over 3 years
    What to do to enable git pull origin master:master which should default to merging. Is this a merge conflict or not. This is the one and only question being asked.
  • siddmuk2005
    siddmuk2005 over 2 years
    This is perfect answer
  • Cezary Drożak
    Cezary Drożak over 2 years
    GitHub was never mentioned by OP. GitHub is a forge for hosting git repositories, not a synonym
  • Maninder
    Maninder about 2 years
    Thanks brother..Worked for me
  • Rajesh Swarnkar
    Rajesh Swarnkar about 2 years
    Could you please add some diagram what does the commands do?