LD01: Using git for Homework due Wed 27 Aug 09:00

Purpose

In this prelab session you will:
  1. create a GitHub account and clone the base repository for this course
  2. set up SSH for command-line Git,
  3. make a local workspace with origin pointing to the instructor repo and mine pointing to your private repo, and
  4. share your private repo with the instructor.

Preparation for Lab Day

Create a GitHub Account and an Empty Private Repo
  1. Create an account: https://github.com/join. Why: You need a GitHub account to host your private repository for submissions.

  2. In GitHub, create a new repository (Repository $\rightarrow$ New) with these settings:
    • Name: p2_homework
    • Description: blank
    • Visibility: Private (important!)
    • Do NOT add README, .gitignore, or license.

  3. Invite the instructor to your private repo (Settings $\rightarrow$ Collaborators): add terrys@gmail.com. Why: This gives the instructor read access to grade your work.

Install Git and Open a Terminal

Read/Watch: https://hsutx.instructure.com/courses/13381/modules/items/436337

Clone the Instructor Base Repository (First!) Read/watch: https://hsutx.instructure.com/courses/13381/modules/items/436336 Why: Cloning the instructor’s repo first makes your history match the instructor’s. This is what allows you to git pull origin for updates all semester. Open a terminal window (Git Bash or Terminal) and type:

cd
git clone https://github.com/tsergeant/p2_homework.git
cd p2_homework
git remote -v       # confirm "origin" points to the instructor repo

Set Your Git Identity (One Time per Computer) Why: Commits are labeled with your name and email.

git config --global user.name  "Your Name"
git config --global user.email "you@example.com"

Set Up SSH (One Time per Computer) Why: SSH lets you push/pull without typing your password each time. The SSH agent holds your key in memory; ssh-add loads your key into it.

# Create an SSH key.
# NOTE: I leave the password blank here so I won't have to type a password for every push/pull.

ssh-keygen -t ed25519 -C "you@example.com"

# Show your public key so you can copy/paste it into GitHub:

# If using MacOS do this command and then highlight it and copy to clipboard:
cat ~/.ssh/id_ed25519.pub

# If using Windows (Git Bash) - copy to clipboard:
cat ~/.ssh/id_ed25519.pub | clip

Add the key in GitHub: Profile $\rightarrow$ Settings $\rightarrow$ SSH and GPG keys $\rightarrow$ New SSH key $\rightarrow$ paste the copied key and remove the email address if present. Then save.

Tip: In the next step it will help to have your repository URL (looks like git@github.com:...) copied to the clipboard. To do that, click on the github icon in the top left and then click on the repository you just created in the left panel. From that repo page you should see a section near the bottom that says “...or push and existing ...”. Copy the URL that starts with git@github.com:... (don't copy the whole command, just the URL).

Connect Your Private Repo and Mirror the Starter Back on your computer in your terminal. Make sure you are in the p2_homework directory. If you're not sure do this:

cd ~/p2_homework

Why: You will pull updates from origin (instructor) and push your work to mine (your private repo).

# Add your empty private repo as "mine" (use the SSH URL from GitHub):
git remote add mine git@github.com:YOUR-USER/p2\_homework.git
git remote -v   # you should now see both origin (instructor) and mine (you)

# First push: make your private repo match the starter (branches + tags)
git push -u mine --all
git push mine --tags

If these steps worked correctly you should be able to refresh the repo page in GitHub and see the starter files.

Video (command-line refresher): This is a good time to watch the following videos and practice the commands presented. You will be needing these commands throughout the semester.

Troubleshooting (Quick)

Lab Day

For lab day you will need your own laptop computer and it will need to have a workspace already setup on it in accordance with the prelab instructions above.

  1. Open the terminal window where you can issue git commands from the command-line.

  2. Verify that you have no uncommitted changes: git status (should indicate that everything is up-to-date). If not then you'll need to commit the changes as follows:
    git add .
    git status
    git commit -m "meaningful message explaining what changes you are committing"
    

  3. Make sure you have the most recent versions of the base and hosted repositories:
    git pull origin main
    git pull mine main
    

  4. Edit the file ParallelArrayMovies.java that is found in the hw01 directory (you can use jgrasp or whatever other editor you normally use when writing Java programs). In the comment block at the top enter your name as author and the current date as version. Then compile and run the program as it is. Enter a new movie and then list all movies to verify it was entered correctly.

  5. Use the command-line to do this step. Switch to the data directory of your repository: (cd p2_homework/data) and then make a copy of the data file (movielist.txt) and name it tinymovielist.txt:

    cp movielist.txt tinymovielist.txt

  6. Edit the new tinymovielist.txt file (use any available text editor) so that most of the movies are deleted leaving only about 10 movies. Save the file.

  7. Continuing editing your Java source file and modify the String constant named DATAFILE so it refers to the new tinymovielist.txt file.

  8. Back at the command line enter the command: git status

    Take time to observe the output. This command will be used often to determine what has changed since the last commit. In this case it should indicate that you have modified a file and created a new file and that both of those actions are “unstaged” ... which means you haven't indicated that you want these changes to be remembered.

  9. We need to tell git that we want our changed to be included in the next commit so you will add them to your staging area with these commands: git add ParallelArrayMovies.java and git add ../data/tinymovielist.txt. Now issue the git status command again and compare the output with the previous result. It should show these changes as having been staged.

  10. Now we are ready to commit. With each commit you will attach a message indicating the changes you are committing. Use this command:

    git commit -m "Created Small Datafile for Testing Purposes"

  11. Issue git status again and verify that there are no uncommitted changes. If you use git log you should see there are now two commits.

  12. We are done making changes to this source code for lab day so let's go ahead and push our most recent changes to BitBucket: git push mine

    When you are prompted to enter a password you will enter your BitBucket password. If successful, your BitBucket repository will reflect all of the changes you just made. So, when you are ready to work on your homework you will begin by pulling the changes from BitBucket into whatever environment you plan to use for working on homework. NOTE: MAKE SURE YOU FINISH THIS STEP!

  13. Summary of git commands we use when we are ready to commit:
    1. git status (to see what has changed)
    2. git add xxxx (as necessary to stage changed/new files to be committed)
    3. git status (to verify everything you want to commit is in the staging area)
    4. git commit -m "Explanatory Message" (to commit the changes)
    5. git status (to verify everything committed as you expected)
    6. git push mine main (to upload the most recent version to your BitBucket account)

  14. Postscript: This lab day is quite a bit different than most will be. In particular the instructions were especially prescriptive. In the future the instructions will not include specific commands to type. The purpose of this lab day was to walk you through the process of working with a VCS. Feel free to refer back to these steps if you don't remember a specific git command.

  15. If you finish early, take time to begin reading the homework assignment. For that assignment you will be need to do your work in whatever environment you set up in the prelab for doing homework.