Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Open source Java projects: GitHub

A guide to social coding with Git and GitHub

  • Print
  • Feedback

Page 3 of 3

Figure 5. Creating a new repository (click to enlarge)

 

Click that link and give your repository a name and description. I created a new repository to host my GeekCap utilities, which is a set of helper classes that include sorting algorithms and a re-sortable list, a class that easily extracts icons from the Java Look-and-Feel Graphics Repository, ZIP utilities, and more. While not the coolest project, I included it because most of my other projects use one or more of these utilities, so it's good to have them stored in an accessible place. I named my project geek-util and gave it a description: "Geekcap Utilities: helpful classes that are used by other Geekcap.com projects."

Once your project is set up you should see a screen like the one shown the Figure 6.

Figure 6. Repository created (click to enlarge)

 

Adding a project using Git commands

The screenshot in Figure 6 shows a listing of what you can do with your new repository, as well as an example of creating a README file and pushing it into your repository. I have an existing Maven project that I need to add for the first time, so I start by adding my pom.xml file and my src directory. Below are the Git commands that I entered for the initial push of the project into the repository:

Listing 1. Git commands for creating a repository

git init
git add src
git add pom.xml
git commit -m 'Initial commit'
git remote add origin https://github.com/geekcap/geek-util.git
git push -u origin master 

Here's where familiarity with Git is important if you want to use GitHub. Fortunately, the main Git commands are relatively intuitive:

  • git init creates an empty Git repository. Specifically, this creates the .git directory, which the git command will recognize as a repository.
  • git add adds files to the repository; in this case I added my pom.xml and my src directory.
  • git commit commits changes to the repository. All I did was to add the pom.xml file and src directory. You would also use this command after modifying the contents of a file or deleting files via the git rm command.
  • git remote add origin adds the specified URL as the origin server for the Git repository. As you saw in Figure 6, the origin server is created on GitHub for you and the URL is provided in the setup documentation.
  • git push uploads all committed changes to the specified server. In this case I've pushed the initial commit that contains the pom.xml and src directory to the origin server, which I previously set.

You can use Git from your IDE or from the command line; I just I happen to be a command-line junkie. Executing git help shows the most common commands, which are summarized in Listing 2.

Listing 2. More common Git commands

usage: git [--version] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [-c name=value] [--help]
           <command> [<args>]

The most commonly used git commands are:add        Add file contents to the index
   bisect     Find by binary search the change that introduced a bug
   branch     List, create, or delete branches
   checkout   Checkout a branch or paths to the working tree
   clone      Clone a repository into a new directory
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   fetch      Download objects and refs from another repository
   grep       Print lines matching a pattern
   init       Create an empty git repository or reinitialize an existing one
   log        Show commit logs
   merge      Join two or more development histories together
   mv         Move or rename a file, a directory, or a symlink
   pull       Fetch from and merge with another repository or a local branch
   push       Update remote refs along with associated objects
   rebase     Forward-port local commits to the updated upstream head
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index
   show       Show various types of objects
   status     Show the working tree status
   tag        Create, list, delete or verify a tag object signed with GPG     

Update and commit

The ability to make changes locally and then push them into the repository is a powerful feature of GitHub. For instance, I recently noticed that my GitHub project geek-util uses JUnit 4.6 and decided to upgrade to v4.10. I started by updating my POM file locally to include JUnit 4.10. In order to review the differences between my local repository and the code I'm working on I executed the git diff command:

Listing 3. Diff review

$ git diff

index d1c3066..5fe2f4f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -63,7 +63,7 @@
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
-            <version>4.6</version>
+            <version>4.10</version>
             <scope>test</scope>
         </dependency>
         <dependency>

The minus sign (-) shows the value of the line in the repository and the plus sign (+) shows the value in my staging environment, so it's obvious that I updated JUnit from 4.6 to 4.10. My next step is to commit the change to my local repository, which I can do with the git add and git commit commands.

Listing 4. Add and commit

$ git add pom.xml
$ git commit -m "Updated JUnit from version 4.6 to 4.10"
[master 413e4ec] Updated JUnit from version 4.6 to 4.10
 1 files changed, 1 insertions(+), 1 deletions(-)

With the commit stored locally my final step is to push this file up to GitHub using git push:

Listing 5. Push to GitHub

$ git push -u origin master
Username: 
Password: 
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 332 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/geekcap/geek-util.git
   ed80e8e..413e4ec  master -> master
Branch master set up to track remote branch master from origin.

On the GitHub website I can now see the updated file, as shown in Figure 7.

Figure 7. Reviewing committed changes (click to enlarge)

 

In conclusion

GitHub is a social networking site and source code hosting repository for software developers dedicated to promoting and supporting open source projects. It allows you to freely host your own open source projects, as well as hosting commercial private projects for a nominal fee. In GitHub you can easily find existing projects based on popularity, search criteria, or programming language preference. You can also follow the progress of software projects and individual developers.

This edition of Open source Java projects has reviewed what GitHub is and how it works, provided a brief overview of the Git distributed version control system, and demonstrated how to create and set up a GitHub repository. I concluded with an example demonstrating the process of modifying, committing, and pushing changes to GitHub.

Hopefully this overview has given you a taste for what Git and GitHub have to offer. There is a lot more to learn, so see the Resources section for additional reading.

About the author

Steven Haines is a technical architect at Kit Digital, currently working onsite at Disney in Orlando. He is the founder of www.geekcap.com, an online education website, and has written hundreds of Java-related articles as well as three books: Java 2 From Scratch, Java 2 Primer Plus, and Pro Java EE Performance Management and Optimization. He lives with his wife and two children in Apopka, Florida.

  • Print
  • Feedback

Resources