Thursday, March 27, 2014

Learning - updating Java Project in GitHub (via Eclipse & GitHub Client)

Yo', so yesterday we finally learned how to create a Java project and upload ("push") our project to GitHub. That's was great!

Today, we will see how to make updates to our Java project. We are working on this simple Java project called "MyFirstApp". We are storing this project in the Git repository called "HeadFirstJava". Anyone can see this @ https://github.com/gradney/HeadFirstJava

If you missed 'howie' (technically, "how we")...
Ok, what was I saying earlier. Ah yes, making updates to our Java project and pushing our updated project into GitHub repo "HeadFirstJava". Here we go...

First, let's begin with a good practice - by seeing if anyone else updated my project in GitHub. If someone did, then I need to see what these updates are and download (or "fetch") the code updates to my local machine (and sync up my local project).

I open my GitHub Client and click the 'arrow' icon on the HeadFirstJava repository (element?)(I would say 'button', but it doesn't look like a button. It's like a hyperlink that highlights it's element block when the mouse is over it). Alright, so we opened this repo directory in GitHub Client and see no files or folders are un-synced.


Great, now we can proceed with making updates in our Java project.

In the book "Head First Java" (by Kathy Sierra & Bert Bates), we read through the pages and reach page 9. This is where my first application comes from. I will add the rest of the code into MyFirstApp program (via Eclipse). Later, I will use an editor as instructed by the authors to write my Java programs. However, this means that we also need to learn later how to use Git via Git Bash (shell commands). Until then, let's continue with the approach we already know - updating Java project in Eclipse and uploading code via GitHub Client app.

On page 9, I need to add the two statements and I do. Next, I save the updates in my project within Eclipse. My Eclipse shows me that MyFirstApp project is now different (i.e. has been updated) due to the ">" icon next to the folder and file icons of my project.


Next, right-click on "MyFirstApp.java", select Team > Commit... Add a Commit message, check the File "MyFirstApp.java" and click Commit. Now our changes are committed to our local Git repo "HeadFirstJava". Again, to upload our code updates, we open our GitHub Client app and see we are out of sync with the online repo (i.e. my GitHub's HeadFirstJava). Our GitHub Client even has a little 'arrow up' with a number inside a blue ellipse shape that notifies us that our local has an updated version compared to the program stored in our online repo. We also see what has been updated in the screen display within the GitHub Client. This is pretty cool!!! I can already see the updates without having to guess. Incredible!


Simply, click on the "sync" button and watch GitHub Client synchronize GitHub. GitHub Client's History column updates with my comments AND GitHub's HeadFirstJava repo is updated with my code. This is remarkable!


Oh crap! You know what we forgot to do? Yep, we forgot to run our program and make sure the code works before we "committed and pushed" our code to our online repo in GitHub. Well, usually we would run and test our program before "committing and pushing", but for now let's just run and test our program locally to make sure it works.


EXCELLENT! Whew, and we're lucky too (that it worked). If it didn't though, we would just make code updates, test the program, and then commit/push to GitHub repo. Well, that's it for today! Good job, pro!

Oh, just in case, here's a link to the Head First Java book @ http://www.ebooks-it.net/ebook/head-first-java-2nd-edition.

Wednesday, March 26, 2014

Learning - pushing Java Project into GitHub (via Eclipse & GitHub Client)

Hello good people!

Today, the goal - is to push a simple Java Project (created in Eclipse) into GitHub.

I already created a quick, simple Java Project in Eclipse.
  • My Eclipse Workspace Directory = C:\Users\gradney\workspace\Git
  • Name of my Java Project = "MyFirstApp"


As you can see, I don't really have any code in my project. I only have one Java class with an empty main(). First, I want to see if I can push my Java project into GitHub before I start coding like crazy and having no where to store a controlled version of my coding. If I have no place to store my code, then one day I might lose my code and have to start all over. Ugh, I've done this before and it REALLY sucks. Hence, why I'm blogging about using GitHub.

So, now that I have a Java project, how do I push this into my HeadFirstJava repo in GitHub?

Let's see if sharing this project in Eclipse to my local Git repo will trigger an automatic update in my GitHub Client App.

-------------------------------------------------------------------------------------------

In Eclipse, open the Git Repository Exploring perspective.
Click on "Add an existing local Git repository".
See a dialog display with your Git root directory and listing repositories existing in your local Git directory.
Check the Git repository to add, and click Finish.


NOTE: If I didn't add my local Git repository in Eclipse using the Git Repository Exploring perspective, I will NOT see any existing Git repos listed when I'm trying to share my project (i.e. Team > Share Project... my project).

In Eclipse within the Navigator view (or Package Explorer view), I right-click on the project "MyFirstApp" and select "Team" > "Share Project...".
In Eclipse's "Configure Git Repository" window, I select my "HeadFirstJava" repository from the drop-down option and see how pretty much everything is already set to go.
  • Project = MyFirstApp (checked)
  • Current Location = C:/Users/gradney/workspace/Git/MyFirstApp
  • Target Location = C:/Users/gradney/git/HeadFirstJava/MyFirstApp
Click "Finish".


I now see question marks on my folder and files that are shared into my Git repo.


Next, I right-click on the project "MyFirstApp" and select "Team" > "Commit".
I add a Commit message, check the file(s) I would like to commit (which is "MyFirstApp.java") and I click "Commit".


*I am NOT clicking on "Commit and Push" because I had issues where my Git repo was not pushing my files up to GitHub. Hence, why I'm going to push my files using the GitHub Client App. See my first blog for my struggles.
**(Oct 16, 2014) Using Eclipse IDE Luna, I was able to setup my Git connection to GitHub without issues and able to click on "Commit and Push" without issues. :) Seems like Eclipse Team fixed this.

Notice the question marks on the folder/file icons have disappeared. This must mean my files are committed to my local Git.


If we open up the GitHub Client App, we see that I have my project added and ready to commit (if I haven't already committed in Eclipse).


Once we already commit our files in Eclipse to the local Git repository, GitHub Client App automatically reflects this and provides additional options (such as reverting, sync'ing to GitHub, etc.).


I want to "push" my project up to GitHub which means I want to publish everything locally in my HeadFirstJava repo to my online repo in GitHub called HeadFirstJava. This is basically sync'ing my local and my online repos. So, I click on the "sync" button.

Then, let's go to my GitHub and see if we achieved our goal for today - adding a Java Project into GitHub.
YES!!! Success!! MyFirstApp is stored in my GitHub repo called HeadFirstJava.


And I can navigate within the project in GitHub to that I uploaded the right code for MyFirstApp project. EXCELLENT!






Tuesday, March 25, 2014

Learning - using GitHub's Client App (for Windows)

Yo,

So, in my last post I just kept running into all kinds of issues with trying to use EGit to create a quick Java Project and push up to my GitHub (on my Windows 7). Today, we try a different approach. Let's go through the client app created by GitHub @ http://windows.github.com/

Before I go through these instructions by GitHub, I still have the following set paths:
  • My GitHub Java Rep:  https://github.com/gradney/Java.git
  • My Git root folder:  C:/Users/gradney/git
    • only thing in this folder is my global core.excludes file:  .gitignore
  • My Workspace folder:  C:/Users/gradney/workspace/Git
    • Although I still use Eclipse, I am not using EGit (i.e. Git plug-in for Eclipse).
I downloaded the "GitHub for Windows" client app. We'll call this GitHub Client. When I clicked on "create" for creating a new repository, I noticed this Client was trying to store my new repos in a new default path as "C:/Users/gradney/Documents/GitHub". Let's change this, in the Client's "tools", to what my git uses:
  1. Updated default GitHub folder to:  "C:\Users\gradney\git"
  2. Updated default shell to:  Git Bash
Instead of creating a local repository, let's sign into GitHub via this Client. I'm officially logged in, but I don't see any difference. I create a new repo:
  1. Name:  HeadFirstJava
  2. Description:  Projects created while learning from Head First Java.
  3. Path:  C:\Users\gradney\git\HeadFirstJava
  4. Push to GitHub:  True (checked)
  5. Keep this code private:  False (unchecked)
  6. Account:  gradney (checked)
Click Create, and I get this nice little window for confirmation. I open this repo and see initial files.



I also see this repo (i.e. HeadFirstJava) created via Windows Explorer.


I go into my HeadFirstJava folder via the Windows Explorer.


Yes, we are making progress! And I see it's already in my GitHub, yes!!!

Okay, what next? Well, let's open the .gitignore file to see its content. Oh great! GitHub already added all the files that should NOT be uploaded into GitHub. Whew, thanks a lot GitHub folk!!!

So, maybe I can create a Java Project with Eclipse now. Before I do, let's add a Summary, Description for Uncommitted changes, and then "Commit changes" in the GitHub Client. Nice, we have "Unsynced commits" which is a good sign. Next, click on "publish" in the GitHub Client first and see what happens.



Yes! I also clicked on "sync" in making sure everything was sync'd. And, my files are committed online as we can see! I'm feeling really good now (especially after all the trouble I had yesterday and this morning).



Oh, let's add the README file as recommended by GitHub. Online, let's click on "Add a README" button. I'm pretty satisfied with what GitHub added automatically into this README file. I add a quick title and description, and then click on "Commit new file" at the bottom of the webpage.

Here's something interesting: I just added a new file via online in GitHub but I don't have this downloaded to my local Git repo. What do I do (in sync'ing the two)?

Well, this is beautiful - GitHub Client already connects and establishes a claim that our local repo is not in sync with the online repo (i.e. GitHub). The nice little, light blue icon (with the number of updates missing from my local) is displayed next to the "sync" button. All we have to do is click "sync" and our local will sync up with GitHub. This also updates in our Git root folder (C:\Users\gradney\git\HeadFirstJava). Perfect!!!



Now, we can proceed towards the main objective - creating a Java Project to publish to GitHub. Hopefully I can put this up tomorrow. Until then, Peace! :)

Monday, March 24, 2014

The Journey Begins - learning Java (in Eclipse IDE) and Git

Hello my good people!

I really wanted to avoid this, but I am starting to realize that the best way to code and remember what I'm coding (along with configurations and all) is through writing down my experiences and listing my references. Blah, wish I could skip all this and just load everything into my fat brain like in the Matrix. Anywho...learning the new kung-fu (i.e. coding) - begins.

Assignment:  Create a simple Java application and push up into my GitHub repo.

  1. I already have a GitHub account.  https://github.com/gradney
  2. I had to reset the password because I haven't used this since July 2013. Sucks to have to start all over.
  3. Login to GitHub is now successful. I didn't like the duplicate "menbuild" in the online path for MenBuild repo (i.e. https://github.com/gradney/menbuild/menbuild) and deleted this repository. I want this path to look like https://github.com/gradney/menbuild.
    1. To delete GitHub repo, see @ https://help.github.com/articles/deleting-a-repository
  4. In starting all over, I decided to redo the simple GitHub tutorial for starters.
    1. See my results @ https://github.com/gradney/Hello-World
    2. What is Markdown (md)? See more @ http://bywordapp.com/markdown/syntax.html
  5. Next, I opened my Eclipse which I had already connected my Git-specific workspace to GitHub and noticed I still had some old stuff. Since I already deleted the "menbuild" repo, I deleted repo (with its contents) from my Eclipse AFTER I copied any useful files to my Desktop. I was left with the "tester" repo. I'll get to this later.
  6. I started running into a lot of issues with trying to use my existing Eclipse workspace (pointing to C:/User/gradney/git) and pushing projects up to my GitHub. Strangely, I didn't run into issues fetching projects down from my GitHub. Anyhow, tired of being constipated (i.e. running into *push* issues).
  7. I decided that I'm deleting my entire workspace and starting all over with Eclipse. One thing I now realized is what may be significant in keeping Eclipse metadata files separate from Git metadata files. What I had done was make my Git folder also the same as my Eclipse workspace (both pointing to C:/User/gradney/git). From reading the following online article, I'll empty my "git" folder and keep my Git default repository pointing here. On the other hand, I'll create a new workspace specifically for Git projects in Eclipse pointing to C:/User/gradney/workspace/Git.
    1. Using Eclipse with Git, see @ http://www.vogella.com/tutorials/EclipseGit/article.html
  8. Now that I'm starting with a fresh setup with Eclipse (including Git plug-in), I will skip adding the .gitignore file for now. I prefer to keep these files within each Git project because I'm sure there will be specifics later on (according to project's purpose and coding language used) and because I would like to upload this file into GitHub for others to know clearly what all is included in my specific Git projects. In my opinion, anyone who would like to use my projects should be able to fork/fetch my projects (sorry, still learning the lingo of Git world) and run on their local machine w/out any issues.
  9. Let's get to the point of this: Creating a Java project to store in GitHub.
    1. Create a quick Java Project. I'm calling mine "Hello-Java". This will be similar to "Hello-World" done through the GitHub starter tutorial, but this is specific to Java using Eclipse.
    2. How am I doing this? I open the Java Perspective in Eclipse, add a Java Project, create the source files, compile and ensure everything works in Eclipse. (That is, I can run my program in Eclipse.)
    3. Once I finish running my program successfully, I open the Navigator View (since Java Perspective typically opens the Package Explorer). In the Navigator View, I can see all my files in this Hello-Java Project. Here is where I'll want to add my .gitignore file mentioned by Vogella (in link above). I want my .gitignore file to be specific to this Java project. I add this .gitignore file in the Hello-Java folder (not the "bin" folder, not the "src" folder, etc.). It's okay if my other .* files are uploaded like the .classpath because this will help others know what might have also been used to compile my project (like for more complex projects I may use Apache libraries, etc.).
    4. I go back to my tutorial by Vogella and follow through with adding my project my GitHub via Git. I configure my Git Repository as "C:/Users/gradney/git/Java/.git", my Working Directory as "C:/Users/gradney/git/Java". So, I see my Project checked as "Hello-Java", my Current Location as "C:/Users/gradney/workspace/Git/Hello-Java", and my Target Location as "C:/Users/gradney/git/Java/Hello-Java". I click Finish and let's see what happens.
    5. Interesting, I see Eclipse added "[Java NO-HEAD]" to my Hello-Java project in the Navigator View. When I go to my git folder via Windows Explorer, I see a Java folder with a ".git" subfolder and a "Hello-Java" subfolder (with my program). When I go back to my project in Eclipse and right-click on my Hello-Java project and go to Team, I see a bunch of Git commands now. Cool! But wait, I don't see my Hello-Java project in my Eclipse Workspace via Windows Explorer. This is quite strange. Let's continue to see what happens.
    6. Oh no, I just realized something. Maybe my idea of where to place the ".gitignore" file needs to be in the parent folder of the project in git? So, I'll copy my ".gitignore" file from the "Hello-Java" project into the git root folder (C:/Users/gradney/git) and add one line to ignore the RemoteFiles directories.
    7. I continued with seeing the Staging view in Eclipse from Vogella's tutorial, but I'm getting anxious to see how to push my local repository to GitHub. So, I moved forward with going to my "Git Repository Exploring" Perspective, right-clicking on Java repo, selected "Push", ....wait let me keep reading through Vogella's tutorial. Oh shit! I can't create a repo INTO GitHub. I always have to create my repo in GitHub first, and then I can add projects to it.
  10. Let's try again later...
  11. Ok, going through Vogella's Tutorial again - using EGit with GitHub (section 21).
    1. As you can see, I wasted a lot of time already. This sucks! But, I did learn something.
  12. Delete my repository in Eclipse via the Git Repository Explorer. Include deleting from workspace, etc. Check via Windows Explorer that the folders and files are deleted. Checked (and .gitignore file is still in my root git folder).
  13. Following through with Vogella's tutorial and creating a Git Repo on GitHub called "Java".
  14. I copy the HTTPS URL from GitHub to add into Eclipse.
  15. Currently, there are no existing projects in Java Repo, but we'll continue without any projects by saying "next" that we want to import all the existing (zero) projects. Or, we'll just cancel.
  16. Ok, this is not helping.
  17. In Eclipse, go to Git Repository Exploring and add a repository by selecting "Clone a Git repository".
    1. URI:  https://github.com/gradney/Java.git
    2. Host:  github.com
    3. Repository Path:  /gradney/Java.git
    4. Protocol:  https
    5. User:  <myUsername>
    6. Password: <myPassword>
  18. When asked about my Local Destination, I have:
    1. Directory:  C:\Users\gradney\git\Java
    2. Remote Name:  origin
    3. everything else is blank.
  19. Click Finish.
  20. Alright, we have a Git Repo from GitHub. Now, to figure out how to add my project in Eclipse to this repo.
  21. Create a new Java Project.
  22. Once created, right-click and select Team > Share Project...
  23. Select "Git", click Next.
  24. Select Repository:  Java - C:\Users\gradney\git\Java\.git
    1. Leave Working Directory as C:\Users\gradney\git\Java
      1. This is our local Git repository (claimed as our Working Directory).
    2. The following should be stated in the dialog "Configure Git Repository"
      1. Project:  Hello-Java
      2. Current Location:  C:/Users/gradney/workspace/Git/Hello-Java
      3. Target Location:  C:/Users/gradney/git/Java/Hello-Java
  25. Click Finish.
  26. Right-click on Hello-Java project, select Team, select Commit...
  27. Add message into Commit Message, select the Files to commit, and click Commit and Push...
  28. Ok, got an error regarding I'm not authorized.
  29. Let's see, this time I followed through with GitHub's instructions on "Create a new repository on the command line".
  30. I click on my Java repository link in GitHub, and now I see a blank README.md file in my repository. Ok, so my commandline to GitHub is working. But, what about my Java Project to GitHub???
  31. Ok, I'm still getting errors. This just doesn't make sense.
  32. After doing some more searching over the Internet, I found this article @ http://stackoverflow.com/questions/14259265/egit-not-authorized
    1. In the instructions, I would add this clarity:
      1. A. To specify credentials individually for each remote
        1. Open Git Repository Exploring perspective.
        2. Open Git Repositories view.
        3. Expand the git repository. In my case, I had to expand Java [master] repo.
          1. You should see a 'subfolder' (for lack of better word) for Branch, Tags, References, Remotes, and Working Directory.
        4. Expand Remotes subfolder (and see 'origin').
        5. Expand 'origin'.
        6. Right-click on fetching the git repository and select "Change Credentials...".
        7. In the dialog, ensure the Repository is pointing to the desired one (in my case it is "https://github.com/gradney/Java.git") and enter your correct username and password. Click OK.
        8. Do the same for pushing to git repository (by right-clicking on pushing to git repository under 'origin', selecting "Change Credentials..." and entering correct credentials).
  33. Ok, now that we finally resolved the issue with connecting EGit to GitHub, let's see if we can finally create a Java Project and push to my GitHub Java Repo.
  34. In Eclipse, go to Java perspective and create the quick Hello-Java project with a HelloJava class. Make sure it runs without errors.
  35. Once created, right-click and select Team > Share Project...
  36. Select "Git", click Next.
  37. This view is already so much better. My dialog titled "Configure Git Repository" does NOT show the local and destination crap from earlier. Instead, simply has "Use or create repository in parent folder of project" checked, and only the view with the Project, Location, and Repository:
    1. Project:  Hello-Java
    2. Location:  C:\Users\gradney\workspace\Git\Hello-Java
    3. Repository:  ..\..\..\.git
  38. I simply click "Finish".
  39. I see Eclipse is "re-indexing repository gradney" in the lower right corner of my Eclipse screen while the bar is loading. This looks like a good sign.
  40. Waiting for indexing to complete...
  41. Yes, seems to have worked!!! In my Package Explorer (in Eclipse's Java perspective), I finally see the question mark (?) next to my Java project files. This means, in git, that I have new/updated files which are not yet committed and pushed to my repo.
  42. However, when I'm trying to stage my project, I'm running into Java Heap Exceptions in my Eclipse (and my Eclipse is locking up). I decided to add my ".gitignore" file into the Hello-Java project. Eclipse is currently re-indexing.
  43. Ok, so my Eclipse keeps running out of memory because it's trying to stage everything with my one Java project. What a drag! I closed my Eclipse. Well, I added a global .gitignore file into my user folder. I added what Vogella mentions in his article.
    1. Location of my global .gitignore file @ C:/Users/gradney/.gitignore
  44. I open my Eclipse and let's see what happens. The Git Staging view has disappeared. Maybe that's a good thing. Let's see if we can right-click on our Java project and commit.
  45. Nope, still running into issues. Ok, this is just too much. Tomorrow, let's just use another Git client, and only use Eclipse for Java projects. How disappointing. I'm very disappointed with EGit. Not sure what happened from last summer until now. Last summer it was working great. Now, it's garbage.
  46. Or maybe this has something to do with my work machine. 
I almost forgot to say some important specs (i.e. specifications). Here's the software I'm using:
  • My operating system is - Windows 7
  • Java 7 - jdk1.7.0_21
  • IDE, not sure of the version, but using - Eclipse IDE Juno
    • don't forget the Git plugin
  • Git version 1.8.1.msysgit.1
  • PSPad (my favorite text editor)