Git HowTo

From CMSC 420
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

What is Git?[edit]

Git is a version control software originally developed by Linus Torvalds (the guy who wrote the first Linux kernel). It is very popular in industry, and even if you don't work with it, chances are a project/library that you want to use is maintained with Git. Unfortunately, Git on the command line is somewhat complicated (and was never intended to be used directly), but there are many good tutorials on the Web that teach you how to use it. Here, we will only discuss working with Git in Eclipse. We will add a remote repository so that your code is backed up in a place other than your laptop's hard drive in case your laptop is the victim of <insert catastrophic event here>.

Eclipse and Git -- EGit[edit]

EGit is the de facto standard plugin for working with Git in Eclipse. It's even included with Eclipse now with the release of Neon. If you have a version of Eclipse earlier than Neon, you'll need to install EGit through Eclipse's update feature, or upgrade your Eclipse installation. The latter is probably the better way to go.

A Note About EGit:

EGit does not like to use Git in the way that you're probably used to, and this creates confusion. Normally, you would clone a repository to your working directory, then command-line git takes care of the rest when you commit, push, pull, etc. EGit likes to keep the local copy of your repository somewhere outside of the project structure.

Getting Started: Creating a Remote Repository and Linking With Your Project[edit]

You should first make a remote repository on a site that allows you to keep private repositories (after all, you don't want to get flagged for academic dishonesty by accidentally putting your project implementation in the public domain before the deadline). Here, we'll use BitBucket.

1) Go to bitbucket.org and create an account

2) Make a new repository, make sure the "private" checkbox is checked. I named my repository "MeeshQuest". Don't add a README, just leave the repository empty.

3) Copy the the URL of the repository (the https one)

Now in Eclipse,

1) Go to Preferences -> Team -> Git -> Configuration

2) Click "Add entry" and make the key "user.name" and the value your name, then hit "OK"

3) Click "Add entry" again, and make the key "user.email" and the value your email.

4) Click "OK" to exit Preferences

5) Now in the menu bar, click Navigate -> Show In -> Git Repositories

6) In the Git Repositories window, click "Clone Repository" (Alternatively, right click in the Git Repositories window and click "Paste Repository Path or URI")

7) Paste in your BitBucket URL if it is not already filled in, and leave the defaults that appear, then hit Next

8) Leave the default option of where to store the git repository locally

9) Click finish, and you'll see the repository appear in the Git Repositories window (you will have to type your password for BitBucket to finish cloning)

10) Now, right click on your project in the Package Explorer, hover over Team, and click "share project"

11) Select your git repository from the dropdown menu and leave the default options for the rest

Now your project is linked with the git repository.

Pushing Changes to Repository[edit]

Now that the repository is configured, you can push your code to it. If you haven't used Git before (or aren't sure exactly how to use it), the basic idea is that you make changes to your files, commit those changes to a "commit", then push that "commit" to the remote location (in our case, BitBucket). Git does much more than this, and you should learn how it really works, but this is all we need for the project. So, first we need to make a new commit.

1) Right click on the project in package explorer, hover over Team, and click Commit. A new window will appear.

2) Drag and drop the files that you want to save to the remote repository from the "Unstaged Changes" window to the "Staged Changes" window (its a good idea to only store the files that are necessary, so you'll probably only want to stick your Java files in the repository and ignore the Eclipse project stuff).

3) Fill in a commit message (it is mandatory) and then click Commit and Push. You may need to type your password for BitBucket a few times if you don't have it saved in Eclipse.

If all goes well, you'll receive a message saying that your commit was pushed. You can verify this by going to bitbucket.org and looking at your repository.