Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CPSC Lab Git Tutorial
1 Introduction
Git is an open source Source Code Management (SCM) system or version control system. In
short, version control allows us to track and label revisions to files. Because of this labeling
ability, we can compare revisions and revert back to any given revision. The revision tracking
and labeling functionality also gives us many more capabilities. Additionally, Git gives us
the options of pushing our changes to a remote server, which gives us an additional backup
of our code. For this lab, we will use Git to help us manage the project.
2 Installation and Set Up
Open https://gitlab.pcs.cnu.edu and create an account using your CNU email address.
Your account login must be .. exactly like your CNU email
address. Once you create your account, verify your email by clicking on the link sent to your
inbox from Gitlab. Log on to PCSE’s Gitlab server.
The remainder of this tutorial assumes you are on your own computer, e.g. not the lab
machines. This software is already set up on the lab machines
If you have not done so already, download Git from https://git-scm.com/downloads.
Once finished downloading, install Git using the standard options (e.g. just click next until
finished).
Now we have set up your Gitlab account, lets set up Git on your computer next. To do so,
open up Git Bash (Windows) or a terminal (Linux and Mac OS) and run:
1. git config --global user.email "my.cnu.email@cnu.edu";
c© Christopher Newport University, 2016
1
2. and git config --global user.name "my name".
This is how we know who authored each commit.
3 Usage
We will now go learn how to clone the lab and push to your Gitlab account. For the purposes
of this tutorial, suppose we want to manage Lab 1 using Git. The first step is to navigate
to https://gitlab.pcs.cnu.edu/groups/cpsc150-students if you are in CPSC 150L or
https://gitlab.pcs.cnu.edu/groups/cpsc250-students if you are in CPSC 250L. On
that page you should see a project called cpsc150l-lab01 or cpsc250l-lab01; click that
project.
On the project page click the fork button as shown in Figure 1, then click your user icon.
Once Gitlab finishes you will be brought to your own copy of the repository.
Figure 1: Forking in Gitlab
After Gitlab finishes forking the repository, we need to change the visibility of it. To do so,
click the gear in the upper right hand corner of the screen and then click “edit project” in
the menu that drops down. You should see a section called “Visibility Level” in the page
that follows. Check “Private” like in Figure 2 and scroll down and click save.
WARNING: Any assignments that are listed as “Public” or “Internal” is an
Honor Code violation.
Figure 2: The proper visibility settings
Next, we need to add your instructor to the project (for grading purposes). Click the gear
again and then click members. In the following page, type your instructors name or Gitlab
2
handle into the “People” box, set their project access to “reporter”, and then click “Add
users to project”.
WARNING: It is an Honor Code violation if you add people other than your
instructor or teammates (if and only if it is an assigned group project).
Go back to the repository’s main page, by clicking its name in the upper left hand corner
of the screen. Look at the URL shown near the top middle of the screen (located below the
star and fork buttons). Ensure that HTTPS is selected and copy the URL (see Figure 3).
Figure 3: Cloning in Gitlab
From here on, you have two options. You can use the Git GUI which is good for getting
you started on Git and getting you used to the workflow, or you can use command line Git
which is much more powerful and is generally the preferred way to use Git. We provide both
options, however we start with the command line Git section.
3.1 Command Line Git
In Git bash or your terminal, type
Terminal:
mkdir git
which creates a folder called git in your user directory (C:\Users\ on Windows
or ~/ on Mac and Linux). Now run
Terminal:
cd git
and then type
3
Terminal:
git clone
and paste the URL you copied at the end (be sure to leave a space between clone and
the URL). Once you pasted the URL, press enter. This will create a local copy of the
repository on your machine. If you run ls or dir, you will see a new directory in the current
folder. Now use cd to enter the repository directory, in this case run cd cpsc150l-lab01 or
cd cpsc250l-lab01.
In both labs, the first exercise is to create a "Hello World" program. If you are in CPSC
150L and are using BlueJ, skip to subsection 4.1, complete that, and then return here. If
you are in CPSC 250L, skip to subsection 4.2, complete that, and then return here.
In jEdit or BlueJ (CPSC 150L) or Eclipse (CPSC 250L), create your HelloWorld.java file.
Now return to your terminal and, in the project directory, run
Terminal:
git add .
This command tells Git to track all of the files and changes you have made thus far. Now
run
Terminal:
git commit -m "Created HelloWorld.java"
This command makes Git log all of the changes you have made and gives you a checkpoint
to which you can return, much like a save in a video game. To test this, make some changes
in HelloWorld.java and save it. Run
Terminal:
git status
This will tell you what has changed since your last commit. It should tell you that HelloWorld.java
was changed. Now run
Terminal:
git reset --hard
4
and then check
Terminal:
git status
It says that no changes have been made, so check your HelloWorld.java file. It should be
at the state it was when you did your initial commit. So far, we have only used Git to clone
and log our changes, so lets push our changes to Gitlab. To do so, run
Terminal:
git push origin master
In this command, Git will push all commits that you have made thus far to Gitlab (when
you clone a repository, the Git server from which you cloned it is named origin). Now all
of your commits are backed up to the Git server.
For this exercise, run git add ., git commit -m "message", git push origin master
each time you add something to HelloWorld.java in order to get a hang of the workflow
(for example, when you declare the class, the main method, and when you finish).
You should commit and push at regular intervals (like whenever you get a new
jUnit test to pass, every 10 minutes, or at the end of class).
Once we pushed our changes, the current versions of the files were uploaded to Gitlab. This
means that if we want to work on the project on a different machine, we just have to set up
Git on the new machine. We then clone the repository, and continue exactly where we left
off. Once we are done working on that machine, we push our changes back to Gitlab.
3.2 Git GUI
This tutorial assumes you are using the GUI that comes packaged with Git. If the user
interface you see looks completely different than the included figures, we recommend you
switch to command line Git.
Open up Git GUI on your machine. You should see a window similar to the one in Figure 4.
Paste the copied URL into the “Source Location” box, and navigate to the directory in
which you wish to store the lab in the “Target Directory” box. After you have filled out that
information, click “Clone”. This process creates a copy of the repository on your machine.
5
Figure 4: How to clone using Git GUI
Figure 5: How a Git repository looks in Git GUI
If it asks you for your Gitlab credentials, provide them. Once it is done cloning, you should
see a window similar to Figure 5.
In both labs, the first exercise is to create a "Hello World" program. If you are in CPSC
250L, skip to subsection 4.2, complete that, and then return here. If you are in CPSC 150L
and are using BlueJ, skip to subsection 4.1, complete that, and then return here.
In jEdit or BlueJ (CPSC 150L) or Eclipse (CPSC 250L), create your HelloWorld.java file.
Once that is done, switch back to the Git GUI window and click the “Rescan” button. This
will look for changes in the repository.
In the “Unstaged Changes” area, you should see “HelloWorld.java” (CPSC150L) or
“src/HelloWorld.java” (CPSC250L). Now click the “Stage Changed” button. This will
6
then move “HelloWorld.java” into the “Staged Changes” area. Next, type “added Hel-
loWorld.java” in the “Commit Message” box and click “Commit”. You now no longer see
“HelloWorld.java” on the window. This tells us that we logged our changes to the repository.
Our next step is to push our changes to Gitlab. To do that, all we need to do is click the
“Push” button. Another window will pop up, all you need to do is click the “Push” button
in this window. Should Git GUI ask for you Gitlab credentials, provide them. Now look at
your repository on Gitlab for the changes you made.
Once you get a hang of the workflow, we recommend that you switch to command line Git
since it is more powerful and versatile.
You should commit and push at regular intervals (like whenever you get a new
jUnit test to pass, every 10 minutes, or at the end of class).
Once we pushed our changes, the current versions of the files were uploaded to Gitlab. This
means that if we want to work on the project on a different machine, we just have to set up
Git on the new machine. We then clone the repository, and continue exactly where we left
off. Once we are done working on that machine, we push our changes back to Gitlab.
4 Instructions for Various IDEs
4.1 Using Cloned Repositories with BlueJ
For CPSC 150L students, open BlueJ.
After cloning, click BlueJ’s “Project” menu and then click “Open Project”. In the dialog
that pops up, select the repository that you just cloned. From here on continue with section 3
using Git from a terminal or Git Bash.
4.2 Using Cloned Repositories with Eclipse
For CPSC 250L students, open Eclipse.
Now that we have cloned the repository, we will create an Eclipse project in the Git reposi-
tory. Open the new Java project wizard, and name your project. Do not click next yet
though! Instead uncheck the “use default location” box and instead enter the location of the
Git repository here. By default this location is C:\users\\git\
(Windows) or ~/git/ (Linux and Mac OS) (see Figure 6). Now you can
click next. If you need to add jUnit to your project: click the “Libraries” tab; then click
7
the “Add Library...” button; click “jUnit”; ensure the version is “jUnit 4”; and then click
“Finish”. If you look in the project files, you will see the lab manual in the root and all of
the test classes in the src directory.
Figure 6: The new project dialog
We can continue section 3 using Git from a terminal or Git Bash.
8