Eclipse GitHub Push Error: master:master [rejected – non-fast-forward]

Helpful? Then DOWNLOAD and RATE my app!

Helpful? Then DOWNLOAD and RATE my app!

So, you’ve run into the following VERY annoying message:

master:master [rejected – non-fast-forward]

If you are a nube to Git, which I certainly am, you have no clue what this means, why it’s happening, or how to fix it.  Also, if you’ve traveled down the same path I have, then you’ve encountered this message after one of the following scenarios:

  1. Used EGit in Eclipse to connect a local repository to an already created repository on GitHub.  After setting correct SSH and then trying to push, you get said message.
  2. Local repository has been pushing to remote repository on GitHub forever and randomly crapped out with the message.

If so, then the problem is most likely that your local and remote repositories are out of sync, and there’s a conflict between some files.  Follow the procedure below to resolve this problem.

  1. First, make sure the most recent changes have been committed. To do this, in Eclipse, right-click on the application’s folder->Team->Commit.  If a pop-up does not say “Up-to-date”, then finish committing the current changes to the local repository.  If it’s already up-to-date, continue.
  2. Try to pull from the remote repository to the local repository; then push to it. To do this, in Eclipse, right-click on the application’s folder->Team->Fetch From Upstream.  If you were able to pull, then push by right-click on the application’s folder->Team->Fetch From Upstream. If unable to pull because of an error, then continue.
  3. At this point, I had to force a push from my local repository to remote repository on Github.  Forcing a push will override any changes on Github with the changes from your local repository! If this is not your goal, DON’T CONTINUE. If it is your goal then do the following:
  • Right-click on application’s Folder->Team->Remote->Configure Push To Upstream
  • Configuration dialog will pop-up->Under “Ref mapping” hit “Advanced” button
  • Push Ref Specifications will pop-up->Select “master [branch]” under “Source Ref” and “Destination Ref”
  • In same window select “add spec”
  • You should now see this update in the “Specifications for push” window; select “Force Push” next to it
  • Select “Finish”

At this point my problem was fixed, but I also didn’t care about throwing  away changes on GitHub.  If you are concerned about this, please keep Googleing because I don’t know how to fix it.

Helpful? Then DOWNLOAD and RATE my app!

Helpful? Then DOWNLOAD and RATE my app!

Advertisements

Connect Eclipse Repository to GitHub using EGit

Helpful? Then DOWNLOAD and RATE my app!

Helpful? Then DOWNLOAD and RATE my app!

I assume that if you’re reading this, you know something about the content/document management system called git. Hopefully you understand what a repository is and how to push from a local repository to a remote one, and then pull from the remote repository to the local one.  If not, read about git here.

Also, I’m assuming that you know what Eclipse is and have installed Egit.  If not, read the EGit installation tutorial here.

So, you know what git is and have used EGit a little, but what is Github? Github is a website that allows its users to create remote repositories that are shared with everybody on GitHub – unless the user purchases a private repository.  Users who are added as collaborators can pull the code from the remote repository on Github to a local repository on their machine, modify it, and then push the updated code back to the repository on Github.  Non-collaborators cannot push changes but they can pull code to a local repository on their own machine and use it as they wish.

The following procedure explains how to use Egit from within Eclipse to create a repository on your local machine for storing and managing an application’s files and their history.  It then explains how to connect the local repository to a remote repository on Github so that other users can access it.  This procedure is the same as described in this YouTube video, but clarifies some confusing steps that are essential to this working.

1. Open Eclipse

2. Create  the “Hello World!” application as explained here.

3. Right-click on the “Hello World” folder in the Package Explorer Window

4. Select Team->Share Project

EclipseGithub

5. In the pop-up window select “Git” then “Next”

EclipseGithub2

6. In the Configure Git Repository window select “Create” next to the Repository dropdown menu.

EclipseGithub3

7. In the Create a New Git Repository window type the parent directory and the name of the folder you’d like.  Both of these are the user’s choice. Select Finish

EclipseGithub4

8. Select “Finish” in the Configure Git Repository window to create the repository in the selected folder.  This will delete the project from the workspace folder and put all of the files in the new repository.

EclipseGithub5

9. There will be a red exclamation mark over the “Hello World” folder in Eclipse with the note [Hello World NO-HEAD].  This is because the repository is empty until the first commit.  To commit the project files to the repository for the first time.  So, commit the files for the first time by right-clicking on the application’s folder->select Team->Commit.

EclipseGithub6EclipseGithub7

10.  In the Commit Pop-up window say “First commit” (or whatever you want) and select all files-my personal preference but you can select what files to commit.

EclipseGithub8

11. There may still be a problem.  If so, right-click on the application’s folder and select “Close Project”.

EclipseGithub11

12. Reopen the project by right-clicking on the application’s project and selecting “Open Project”.

13. Select Project in the Eclipse menu, then “Clean Project”.  Select the application’s folder and hit “OK”.  The errors should be gone.

EclipseGithub12

14. The project is now using the local repository to store its files and is able to track changes-though the user must commit in order for changes to be saved to the repository.

15. In order to synchronize the local repository to Github, you need to generate an SSH key. In the Eclipse menu select Window->Preferences->General->Network Connections->SSH2.  Then select the “Key Management” tab and click the “Generate DSA Key”.  This will populate a key in the window below the buttons.  If you can’t see it, expand the window-mine was gone until I maximized the window.  Copy this key into the clipboard.  Select “Save Private Key”, select the destination folder (I used the default folder) and select “OK”.

EclipseGithub14

EclipseGithub15

EclipseGithub16

16. Go to Github.com and sign into your account.  If you have not created an account, do so now.

17. Select the “Add New Repository” button at the top right of the screen.

18. Type in “Hello World” as the new repository name and make it “Public”-unless you would like to pay for it, then you can make it private. Then select “Create Repository”.

EclipseGithub17

19. The new repository has been created and the path for it is listed under the header “Quick Setup.”  Select the “copy to clipboard” icon to the right of the repository path name-and don’t copy anything else to the clipboard!

EclipseGithub18

20. Go back to Eclipse and open the Repository Browser by selecting Window->Show View ->Git Repositories

EclipseGithub13

21. In the Repository Browser expand “Hello World”, right-click on “Remotes”, and select “Create Remote”.

EclipseGithub19

22. Call your new remote “origin”, select “Configure Push” and hit “OK”.

EclipseGithub20

23. In the “Configure Push Window” select “Change” under URI.  This will open the “Destination Git Repository” window.

EclipseGithub21

24. In this window you should automatically see the new repository information populated.  If not, it’s because you copied something else to your clipboard, so close the window and copy the repository link from Github again. Select “SSH” under “Protocol” and select “OK”.

EclipseGithub22

25. The “Hello World” app should now be pushed to Github and you should be connected.

NOTE: If the pushed failed due to:

master:master [rejected – non-fast-forward]

Then see my post on fixing this issue.

Helpful? Then DOWNLOAD and RATE my app!

Helpful? Then DOWNLOAD and RATE my app!