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 in Android Hello World Example
3. Right-click on the “Hello World” folder in the Package Explorer Window
4. Select Team->Share Project
5. In the pop-up window select “Git” then “Next”
6. In the Configure Git Repository window select “Create” next to the Repository dropdown menu.
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
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.
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.
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.
11. There may still be a problem. If so, right-click on the application’s folder and select “Close Project”.
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.
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”.
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”.
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!
20. Go back to Eclipse and open the Repository Browser by selecting Window->Show View ->Git Repositories
21. In the Repository Browser expand “Hello World”, right-click on “Remotes”, and select “Create Remote”.
22. Call your new remote “origin”, select “Configure Push” and hit “OK”.
23. In the “Configure Push Window” select “Change” under URI. This will open the “Destination Git Repository” window.
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”.
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 Eclipse GitHub Push Error: master:master [rejected – non-fast-forward] on fixing this issue.