Lab: Version Control with Git

Steven J Zeil

Last modified: Apr 23, 2016
Contents:
1 Gitting Started
1.1 Fetching changes
1.2 Committing changes
2 Coping with Conflict
3 Finishing Up

This is a self-assessment activity to give you practice in working with the git version control system. Feel free to share your problems, experiences, and choices in the Forum.

1 Gitting Started

Fire up your installation of Eclipse.

Let’s let it know about your key. Go to Window Preferences General Network Connections SSH2 General. Add your private key.

Current versions of Eclipse have the Eclipse plugin for Git already installed. Take a moment and browse the EGit User Guide.

Open an ssh session on one of the CS Linux servers. Give the command

~zeil/Assignments/cs350/gitLab/step1

You will be given the URI of a git repository.

In Eclipse, go Window Open perspective... and open the Git perspective. Move your mouse across the controls at the top of the Git repositories view and select “Clone a git repository…”. Enter the URI that you were given. Fill in your user name, but leave the password blank. Click Finish.

Look in your git repository folder. You should see that a new directory has been added. Look in that directory (use -a on Linux). You will see that it contains a .git directory, which is where the version control info is kept, and a directory for the real project.

In Eclipse, follow the usual procedure for creating a new Java project, but instead fo the default project location, select that project directory (the one alongside the .git directory, not the one that contains it).

Look around in the project. try compiling it. Don’t make any changes yet.

1.1 Fetching changes

Back in your ssh session, give the command

~zeil/Assignments/cs350/gitLab/step2

This is simulating someone else on your team working on the same project. In Eclipse, right-click on your project and select Compare with... HEAD revision. This compares against the most recent check-in to your local copy of the repository. Since you have not made any changes yet, you should not see much of anything, maybe a few new files created when you compiled.

Now try Compare with... branch, tag, … Select “Remote tracking” and then “master”. This compares against the remote repository, and you should be able to see that your mysterious teammate has checked in some changes.

In the synchronization view, find the file that has changed and double-click on it to open up the comparison editor.

Most of the version control commands are accessed by right-clicking on the project and looking under the Team menu item. Return to the Java perspective and use that to the changes from the remote repository.

1.2 Committing changes

Try making some simple changes, such as editing some of the comments in the source code.

From the Team menu item, commit your changes. As you examine the “commit” dialog, you will see that you have the option of doing a simple local commit or of doing a commit followed by an immediate push to the remote repository. Go ahead and push.

2 Coping with Conflict

Back in your ssh session, give the command

~zeil/Assignments/cs350/gitLab/step3

In your Eclipse session, refactor the Pie.getBaseColor and Pie.setBaseColor functions, renaming them to getBackgroundColor and setBackgroundColor. By using the “refactor” menu, you get all calls to that function to be changed at the same time.

Now try to commit and push your changes. The commit should work, but the push fails because your mysterious teammate has already checked in some more changes to the project.

See if you can resolve this conflict and eventually commit and push a combination of your changes and those of your teammate. You will need to

3 Finishing Up

The remote repository for this lab was stored in your Linux account as ~/.cs350gitlab.

To clean up after the lab (or to reset to the beginning if you want to start it over from the beginning), do

rm -rf ~/.cs350gitlab

While you still have those keys handy, now would be a good time to go to the course forge, log in to GitLab. Go to your Profile settings, select “SSH Keys”, and add your public key. This is how GitLab will know that your Eclipse IDE is allowed to work with your project. (Remember, you have already told Eclipse about the location of your private key. So, between the two of them, they have enough information to validate your credentials.)