Due Wed, 09/09 at 11:59pm
Credit: This activity was designed by Semmy Purewall
Learning Goals
By the end of this lab, you should be able to:
- create and use a local Git repository
- stage, commit, inspect, and compare changes
- connect a local repository to GitHub and push your work
- use a branch and pull request to make a small change
Overview
This lab uses one repository: lab03-exercises.
You will create it on your computer first, then connect it to GitHub later in the lab. Git works on your computer without GitHub; GitHub is simply an online home for a Git repository.
You will use one file, README.md, for both project notes and written responses. There is no separate answers.md file.
The programming work in this lab is intentionally small. You already practiced the duplicate-checking problem in Lab 02. Here, you will reuse one of those files so that you can focus on the Git workflow.
Part 1: Create Your Local Repository
-
Open a terminal and move to your
csci338directory. Create a directory for this lab, move into it, and initialize Git:mkdir lab03-exercises cd lab03-exercises git init -b maingit initturns the current directory into a Git repository. It creates a hidden.gitdirectory where Git stores the repository’s history and settings. -
List the contents of the directory, including hidden files:
ls -laYou should see the hidden
.gitdirectory. -
Create a file named
README.mdand add the following starter content:# Lab 03: Git and GitHub ## README Responses ### 1.1 After initialization ### 1.2 First git status ### 1.3 After the first commit ### 1.4 git log ### 1.5 git diff Paste the `git status` and `git diff` commands and their output. How does this `git status` differ from the one in **1.2**? ### 1.6 Git command reflections In one or two sentences each, what does each command do? - `git init` - `git status` - `git add` - `git commit` - `git log` - `git diff` ### 1.7 Repository link ### 1.8 Comparing approaches In your own words: - How does the nested-loop approach check for a duplicate? - How does the set-based approach check for a duplicate? - What is the runtime and memory trade-off of each? ### 1.9 Pull request merge options In your own words, what does each GitHub merge option do? - Create a merge commit - Squash and merge - Rebase and merge -
In
README.md, under 1.1 After initialization, paste thels -lacommand and its output. Use a fenced code block, like this:```text ls -la ...your output here... ```
Before moving on
lab03-exercisesls -la shows a .git directoryREADME.md with the starter headingsPart 2: Make Your First Commit
-
Check your repository’s state:
git status -
Copy that
git statusoutput now, before you change anything else. InREADME.md, find the heading 1.2 First git status (in the starter outline from Part 1) and paste the command plus that output under it. -
Stage and commit
README.mditself:git add README.md git commit -m "Create lab README" -
Run these commands:
git status git log --onelineIn
README.md, under 1.3 After the first commit, paste thegit statuscommand and its output. InREADME.md, under 1.4 git log, paste thegit log --onelinecommand and its output.When
git statussays your working tree is clean, it means Git sees no uncommitted changes.
Before moving on
git status under 1.2 before my first commitgit log --oneline shows the "Create lab README" commitREADME.md have the post-commit git status and git log outputPart 3: Make and Inspect a Change
-
Add this sentence under the title at the top of
README.md:This repository documents my practice with local Git, GitHub, branches, and pull requests. -
Before staging the change, run:
git status git diff -
In
README.md, under 1.5 git diff, paste thegit statusandgit diffcommands and their output. Then answer:- How does this
git statusdiffer from the one in 1.2?
- How does this
-
Stage and commit the change:
git add README.md git commit -m "Describe repository purpose"
Before moving on
README.mdgit status / git diff output and a short answer comparing this git status with 1.2"Describe repository purpose"Part 4: Bring Forward One Existing Program
You do not need to solve a new programming problem for this lab. Reuse one completed program from Lab 02:
ContainsPair.java, orcontains_pair.py
-
Create either a
java-practiceorpython-practicedirectory inlab03-exercises, then copy your chosen Lab 02 file into that directory. Use only one language for this lab. -
Run the program to confirm it still works.
-
Create a
.gitignorefile at the root of yourlab03-exercisesfolder containing this line:*.classThis prevents Java’s compiled
.classfiles from being committed. It is fine to include this file even if you choose Python. -
Stage and commit the program and
.gitignorefile:git add . git commit -m "Add duplicate-checking practice program"
Before moving on
java-practice/ or python-practice/ (not both) with my Lab 02 file.gitignore at the repo root contains *.class.gitignorePart 5: Add Your Reflection
-
In
README.md, under 1.6 Git command reflections, answer in one or two sentences each: what do the followinggitcommands do (do not use GenAI for this):git initgit statusgit addgit commitgit loggit diff
-
Commit your reflection:
git add README.md git commit -m "Add Git command reflection"
Before moving on
git init, git status, git add, git commit, git log, and git diff in my own wordsPart 6: Connect Your Repository to GitHub
Now create an online home for your local repository.
-
On GitHub, create a repository named
lab03-exercises.- Make it public.
- Do not initialize it with a README,
.gitignore, or license. Your local repository already has those files.
-
After GitHub creates the empty repository, copy the SSH URL shown under push an existing repository from the command line. It will look like this:
git@github.com:your-github-username/lab03-exercises.git -
From your local
lab03-exercisesdirectory, connect the remote and push your work. Replaceyour-github-usernamewith your own GitHub username:git remote add origin git@github.com:your-github-username/lab03-exercises.git git push -u origin main -
Check the connection:
git remote -v git statusReload the repository page on GitHub. You should see your
README.md, program file,.gitignore, and commit history. -
In
README.md, under 1.7 Repository link, paste the URL to your public GitHub repository. Commit and push that update:git add README.md git commit -m "Add repository link" git push
Before moving on
lab03-exercises with no extra README / licensegit remote -v shows my SSH origin URLREADME.md, .gitignore, my program, and commit historyPart 7: Make a Change on a Branch
So far, you have committed directly to main. Next, you will make a small improvement on a separate branch.
-
Create and switch to a branch named
more-tests:git switch -c more-tests -
Confirm that you are on the new branch:
git status -
Make these two changes on
more-tests:-
Add one new test case to the Java or Python program you brought forward from Lab 02.
- A test case is one additional program input, its expected result, and a way to see whether the actual result matches your expectation.
- You are adding another call to your existing program, not rewriting its duplicate-checking method or function.
-
In
README.md, under 1.8 Comparing approaches, answer:- How does the nested-loop approach check for a duplicate?
- How does the set-based approach check for a duplicate?
- What is the runtime and memory trade-off of each?
-
-
Run your program again to verify the new test case. Then commit the changes:
git add . git commit -m "Document duplicate-checking approaches" -
Compare the branch with
main:git diff main..more-testsNotice that this command shows the changes that exist on
more-testsbut not onmain. -
Push the new branch to GitHub:
git push -u origin more-tests
Before moving on
git status showed I was on more-tests (not main) while making these changesmore-tests to GitHubPart 8: Create and Merge a Pull Request
-
On GitHub, open a pull request with:
- base:
main - compare:
more-tests
- base:
-
Write a short pull-request description using Markdown. Include:
- a bullet list describing your two changes
- a code block showing the command you used to run your program
- a link to a relevant resource, such as the Pro Git book – to practice making Markdown links.
-
Before merging, use GitHub’s branch selector to look at both branches and confirm that
more-testscontains the changes you expect. -
Still on the pull request page, click the merge button’s dropdown (do not merge yet). Read GitHub’s descriptions of the three options. Then in
README.md, under 1.9 Pull request merge options, answer in your own words: what does each option do (Create a merge commit, Squash and merge, Rebase and merge)? -
Commit and push that README update to
more-tests(this adds a new commit to the open pull request; you still have not merged):git add README.md git commit -m "Explain pull request merge options" git push -
Back on GitHub, refresh the pull request and confirm the new commit appears. Now merge: choose Rebase and merge. Delete the remote
more-testsbranch when GitHub offers.
Before moving on
more-tests into main with a Markdown descriptionmore-tests branchPart 9: Update Your Local Repository
Merging on GitHub does not automatically update the copy on your computer.
-
Fetch information from GitHub:
git fetch -
Run
git statusandgit branch. Notice what changed locally and what did not change yet. -
Switch to
mainand pull the merged work:git switch main git pull -
Delete your local feature branch:
git branch -d more-tests -
Run one final check:
git status git log --onelineYour local
mainbranch should now include the work that was merged through the pull request.
Before moving on
git fetch, then git switch main and git pullmore-tests branchgit log --oneline on main includes the work from the pull requestWhat to Submit
Submit the link to your merged pull request in Canvas. Before submitting, confirm all of the following:
lab03-exercises and connected it to GitHub.README.md, .gitignore, and one reused Lab 02 program.README.md includes my command observations, reflection, repository link, approach comparison, and merge-option explanations.more-tests branch, made and pushed a small change on it, and opened a pull request into main.main branch includes the merged changes.