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 rm -r $HOME/.config/google-chrome/Singleton*

Working in the Lab

You have a choice, either

Lab Prep

Git Basics

What it does

  1. Keeps versions of your project (project history)
  2. Keeps backups remotely (copy of your project on another system)
  3. Enables collaboration (multiple people can work on project)

A Git repository (repo) - consists of

  1. WORKING DIRECTORY: This is where you do your work.
  2. 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.
  3. 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.
References: Getting Started - What is Git?.

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
LOCAL GIT REPO +------------------------------------------------------------+ | | | +----------------git checkout-------------+ | | | | | | V | | | WORKING DIRECTORY STAGING AREA GIT DIRECTORY | | | ^ | ^ | | | | | | | | +----git add--------+ +---git commit----+ | | | +------------------------------------------------------------+

Example, local repo

git init myLocalRepo cd myLocalRepo ls -al # notice the .git directory ls -al .git git status echo 'one' >> myFile cat myFile git status git add -A # whole WORKING DIRECTORY is staged but not committed git status git commit -m 'commit message for c1' git status git log # notice HEAD on branch master echo 'two' >> myFile git status cat myFile git add -A git status git commit -m 'commit message for c2' git status git log # notice HEAD on branch master echo 'three' >> myFile git status cat myFile git add -A git status git commit -m 'commit message for c3' git status git log echo 'four' >> myFile git status cat myFile git add -A git commit -m 'commit message for c4' git log git checkout <COMMIT_ID> # checkout commit c2 cat myFile # see!!! git log git log master # not where HEAD is, vs the last commit on branch master # You can play around in the WORKING DIRECTORY, but git wants you to clean up before going back # This is bruteforce, not always appropriate!!! Alternatives include git stash and others... git clean -d -x -f git reset --hard # now HEAD back to the last commit on master git checkout master cat myFile git log

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.
LOCAL GIT REPO +------------------------------------------------------------+ | | | WORKING DIRECTORY STAGING AREA GIT DIRECTORY | | | ^ | | | | | | | | | | git push git pull| | | | | +-----------------------------------------------+-------+----+ | | | | | | REMOTE GIT REPO | | +-----------------------------------------------+-------+----+ | | | | | V | | | WORKING DIRECTORY STAGING AREA GIT DIRECTORY | | | +------------------------------------------------------------+ References: The Git book

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 cd ~ pwd # to verify that you are in the root of your home directory ls repo_* You should see nothing returned. Now lets get a local copy of the repo, to do that ...
  1. 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.
  2. 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.
  3. Notice that there is now a repo_$USER directory in your main directory.

Adding a File

cd repo_$USER # to change directory to your WORKING DIRECTORY ls # you should see the files and directories we have prepared for you ls -al # notice the .git directory. Please don't go into or mess this up!!! git status # you should see that your repo is up to date cd labs cd lab01 ls # you should see the files inside this weeks lab, in particular # AddNums.java ComedianProgram.java congrats.txt SadComputer.java touch myFirstFile # this creates a new, empty file git status # myFirstFile is not yet part of the STAGING AREA git add myFirstFile git status # myFirstFile is now part of the STAGING AREA git commit -m 'my very first commit' # git now has a record of myFirstFile in the GIT DIRECTORY # mcsscm.utm.utoronto.ca does not have a copy of myFirstFile!! git push origin master # Copy the local commited files to REMOTE GIT REPO mcsscm.utm.utoronto.ca

Summary

If you are working in the terminal, you will continually follow this pattern... cd ~/repo_$USER # no you don't need to clone it again git pull # get a latest copy of the repo from the remote # MODIFY FILES IN INSIDE repo_$USER (your WORKING DIRECTORY) cd ~/repo_$USER git add --all . # instead of adding individual files to the STAGING AREA git commit -m 'A MESSAGE ABOUT THE CHANGES MADE' # to add to the GIT DIRECTORY git push # To synchronize your GIT DIRECTORY with the REMOTE REPO's GIT DIRECTORY So the pattern is: clone while WorkingOnProject: pull make changes add commit push

Java

HelloWorld2

Add class HelloWorld2 also with a main method so that when run, this class prints out "Hello World 2!!!". Hints... gedit HelloWorld2.java javac HelloWorld2.java java HelloWorld2 ls # do you see what you expect # HelloWorld.java HelloWorld.class, HelloWorld2.java HelloWorld2.class, myFirstFile pwd # are you in the right directory? git add --all . git commit -m 'Finished Hello World2' git push

How can I see the history of my local repo operations

git log and type q to quit.

How do I check if I have committed everything and pushed to the remote mcsscm.utm.utoronto.ca?

# go into your git repo and run git status # you should see: "nothing to commit, working tree clean" # alternatively you could... cd ~ mkdir checking cd checking git clone https://mcsscm.utm.utoronto.ca:9999/git/207/25f/repo_$USER.git # now take a look around, is everything there? Note that you now have # two copies of the repo. One in ~/repo_$USER and another in ~/checking/repo_$USER # IF THIS IS NOT THE CASE ASK YOUR CLASSMATES AND TA ASAP!!!!!

(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.

Playing with other Java Programs

cd ~/repo_$USER/lab01 ls # now gedit each of the .java programs we have supplied to you # try to understand what they are doing # compile and run them (javac and java) to see if they do what you think