Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git rejected push non-fast-forward

I am quite new to git, and I had been working on a small side project for the last 2 months and had been pushing stuff onto bitbucket with no problems. A couple of days ago, I zipped my project folder (since I had to reinstall my Linux OS) and now unzipped this after my reinstallation of Linux OS.

So, now, I went to my project folder, kept happily working and finally did:

git add -A && git commit -m "modified code" && git push origin master

..which is what I usually do..

and I get:

To https://[email protected]/johnsproject/proj.git
! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://[email protected]/johnsproject/proj.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.

I have had a look at a few SO questions where they suggest the use of a force flag -f - but I am unsure if I should be doing this.

p.s: I am on the master branch - which is the only branch on my repo.

Would really appreciate if anyone could point me in the right direction here.

Thanks.

like image 765
JohnJ Avatar asked Jun 03 '13 14:06

JohnJ


People also ask

How do you resolve a non-Fast-Forward rejection?

If you do a commit in one project and then accidentally push this commit, with bypassing code review, to another project, this will fail with the error message 'non-fast forward'. To fix the problem you should check the push specification and verify that you are pushing the commit to the correct project.

What is rejected non-fast-forward?

Git push rejected non-fast-forward means, this error is faced when git cannot commit your changes to the remote repository. This may happen because your commit was lost or if someone else is trying to push to the same branch as you. This is the error you face.

Why is Github rejecting my push?

A commit gets rejected and causes a failed to push some refs to error because the remote branch contains code that you do not have locally. What this means is that your local git repository is not compatible with the remote origin. Based on the above, your local machine is missing commits C and D.


2 Answers

There are changes in the central repository that you must pull before you can push. Do

git add -A
git commit -m "my local changes" 
git pull

Resolve any conflicts. Then do

git push

Alternatively, if you have no valuable modifications locally, you can create a new clone of your repo, and start working from there:

git clone https://[email protected]/johnsproject/proj.git new_repo_dir
like image 171
Klas Mellbourn Avatar answered Sep 19 '22 05:09

Klas Mellbourn


Try doing

git pull origin master
git add -A
git commit -m "modified code"
git push origin master

Your local repository is likely out of sync with the remote repository.

like image 24
Ganye Avatar answered Sep 20 '22 05:09

Ganye