In this prelab session you will:
- create a GitHub account and clone the base repository for this course
- set up SSH for command-line Git,
- make a local workspace with origin pointing to the instructor
repo and mine pointing to your private repo, and
- share your private repo with the instructor.
Create a GitHub Account and an Empty Private Repo
- Create an account: https://github.com/join.
Why: You need a GitHub account to host your private repository for submissions.
- In GitHub, create a new repository (Repository
New) with these settings:
- Name:
p2_homework
- Description: blank
- Visibility: Private (important!)
- Do NOT add README, .gitignore, or license.
- Invite the instructor to your private repo (Settings
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
- Windows (Git Bash): Install Git for Windows (includes Git Bash) from https://git-scm.com/download/win.
Why: Git Bash gives you a command-line where you will run all Git commands.
Open Git Bash from the Start Menu.
- macOS (Terminal): Press Command + Space, type Terminal, press Enter.
Run
git --version. If macOS prompts to install “Command Line Tools”, click Install.
Why: Terminal is the built-in command-line; the tools include Git.
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
Settings
SSH and GPG keys
New SSH key
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)
- Permission denied (publickey): Confirm your SSH key is added in GitHub, and that you are using the SSH URL (
git@github.com:...), not HTTPS.
- Wrong branch name: If the default branch isn’t
main, replace main in commands with the repo’s default.
- Instructor can’t access: Verify you invited terrys@gmail.com as a collaborator and the invite was accepted.
- Started the wrong way (fork/template/empty repo first): Delete that local folder, then repeat from “Clone the Instructor Base Repository”.
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.
- Open the terminal window where you can issue git commands from the
command-line.
- 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"
- Make sure you have the most recent versions of the base and hosted repositories:
git pull origin main
git pull mine main
- 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.
- 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
- 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.
- Continuing editing your Java source file and modify the String constant
named DATAFILE so it refers to the new tinymovielist.txt file.
- 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.
- 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.
- 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"
- 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.
- 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!
- Summary of git commands we use when we are ready to commit:
- git status (to see what has changed)
- git add xxxx (as necessary to stage changed/new files to be committed)
- git status (to verify everything you want to commit is in the staging area)
- git commit -m "Explanatory Message" (to commit the changes)
- git status (to verify everything committed as you expected)
- git push mine main (to upload the most recent version to your BitBucket account)
- 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.
- 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.