Lab 1 (Week 1) Unix, git, Java
Submit by adding, committing and pushing by midnight, Sunday night. Your goal should be to finish either in lab or with an additional hours work.IMPORTANT: While you are in the lab, login in to a lab desktop, bring up chrome and visit this attendance app This will register your attendance in the lab.
If you are having problems with your chrome browser on the lab systems, you may have left a chrome session running. To delete it, open up a command prompt on the lab system and run
Working in the Lab
You have a choice, either- Login directly on a lab machine
- Share a lab machine with someone else (directly logging in)
- ssh into a lab machine:
ssh UTORID@dh20XXpcYY.utm.utoronto.ca
Lab Prep
- Learn basic Unix commands, for example The Ten ESSENTIAL UNIX Commands
- We will be learning Java in CSC207, you should start at the java tutorial and in particular, for the first tutorial ... this
- In addition, you should take a look at Getting Started - What is Git?, what is version control and git
Git Basics
What it does
- Keeps versions of your project (project history)
- Keeps backups remotely (copy of your project on another system)
- Enables collaboration (multiple people can work on project)
A Git repository (repo) - consists of
- WORKING DIRECTORY: This is where you do your work.
- STAGING AREA: These are files from the WORKING DIRECTORY that are in their final state, just getting ready for them to be collected up with others, so they can be added to the history in the GIT DIRECTORY.
- GIT DIRECTORY: this is where the history of the WORKING DIRECTORY lives as well as other git administrative details. This is in the .git directory.
Local git commands
- git init:
- create a git repo. This is just a directory with a .git subdirectory, managed by git commands. In general, you do this once, then work in the WORKING DIRECTORY.
- git add:
- Add specified WORKING DIRECTORY files to the STAGING AREA. The developer intends these to be part of the next commit.
- git commit:
- Version the files in the STAGING AREA. This records the present state of the application by storing them in the GIT DIRECTORY.
- git checkout:
- Checkout a particular commit, switch HEAD to the commit and update WORKING DIRECTORY.
- git log:
- See the history of the commits from HEAD back
- git log master
- See the history of the commits for branch master
- git clean -d -x -f
- Clean the working directory of any files and directories not tracked by git
- git reset --hard
- reset WORKING DIRECTORY so that tracked files have their state in HEAD
Example, local repo
Local and Remote Repos
You can keep a LOCAL GIT REPO and REMOTE GIT REPO synchronized using a few additional git commands.- git clone:
- copies the remote GIT DIRECTORY to a new local LOCAL GIT REPO and then checks out the latest state of the files to the WORKING DIRECTORY
- git push:
- push the state of the LOCAL GIT DIRECTORY to the REMOTE GIT REPO. New changes that have been committed locally will be available in the remote.
- git pull:
- pull the state of the REMOTE GIT REPO to the LOCAL GIT REPO. New changes that have been committed to the remote will be available locally.
Git
We have created repositories for you, that is, a place for you to store and share your files. We will be talking about repos more in class. For now, note that you have space on another system with files already populated in it. You will want to copy them to your local lab system. First note that you don't yet have a copy of the repo in your current directory. To do this, bring up a command prompt (terminal) then do the following-
git clone https://$USER@mcsscm.utm.utoronto.ca:9999/git/207/25f/repo_$USER.git # NOTE: $USER will automatically be replaced by your UTORID in the command prompt on our systems. # From home, replace $USER with your UTORID. # The above command copies the git repo from a different machine (mcsscm.utm.utoronto.ca) # to your machine. All code you submit for this course must be committed # to mcsscm.utm.utoronto.ca using git. - Possible problem: git may complain as follows:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: POSSIBLE DNS SPOOFING DETECTED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ The RSA host key for cslinux.utm.utoronto.ca has changed, and the key for the corresponding IP address 142.150.1.123 has a different value. This could either mean that DNS SPOOFING is happening or the IP address for the host and its host key have changed at the same time. Offending key for IP in .../.ssh/known_hosts:11 If so, edit the known_hosts file and remove the offending line, in my case, line 11. - Notice that there is now a repo_$USER directory in your main directory.
Adding a File
Summary
If you are working in the terminal, you will continually follow this pattern...Java
- Please familiarize yourself with a few common unix commands...
ls, cd, pwdand at least one text editor. We have gedit, vim, nano available in the lab. We have others as well, but lets start with one of those.cd ~/repo_$USER git pull cd labs cd lab01 gedit HelloWorld.java Now enter the following in the text editor...public class HelloWorld { public static void main(String [] args){ System.out.println("Hello World!!!"); } } - Save the files contents
- Bring up another terminal window and
cd ~/repo_$USER/lab01 pwd # what does this do? ls # do you see what you expect? cat HelloWorld.java javac HelloWorld.java # to compile your first program. No message means everything is OK # NOTE: If javac is not installed in the lab, # you can also...try (for jdk 11)... # # java HelloWorld.java # ls # notice the HelloWorld.class file, the result of the above command java HelloWorld # You should see "Hello World!!!" Now quit gedit, then add, commit and push your changes.# in the repo directory... git add --all . git commit -m 'Finished Hello World' git push
HelloWorld2
Add class HelloWorld2 also with a main method so that when run, this class prints out "Hello World 2!!!". Hints...How can I see the history of my local repo operations
How do I check if I have committed everything and pushed to the remote mcsscm.utm.utoronto.ca?
(Advanced): How do I check if the remote and my repo are in sync
We won't cover this in tutorial.Whats the problem? I thought we just covered this? Answer: Someone else on your team could have committed and pushed to the remote ahead of you.
Git status won't show you that the remote is ahead of your local repo. You have a few options
(Advanced): What if I modified files and I change my mind
We won't cover this in tutorial.There are different ways of undoing changes depending on where in the process you are. See, for example this.