Assignments > Lab 2: Version Control and Branch Management with git and GitHub
Due on Wed, 09/06 @ 11:59PM. 6 Points.
Introduction
Today we will be practicing collaborating on code by using git and GitHub. Here are today’s slides.
Lab Readings
required | Collaborating using Git and GitHub: Branches, Pull Requests, Merging vs Rebasing (Video walkthrough) |
recommended | What is git rebase? |
recommended | Article explaining how to rebase + handle merge conflicts |
Your Tasks
1. Add your GitHub username to the spreadsheet
If you haven’t already, please register for a GitHub account, and then add your full name and your GitHub username to this spreadsheet. Semmy and I will invite you to be a contributor to the relevant repos.
- Note that you will have to confirm this invitation via email.
2. Set up public / private key authentication for GitHub (and Arden)
When accessing a remote server (including a GitHub server), a common authentication strategy involves using public and private keys. Below, you will go through the process of generating a public / private key. Your private key is for you and you alone. It is your secret, and should not be shared with anyone. Your public key, on the other hand, is typically copied to a server for which you have access.
In the workflow outlined below, all commands should be run from the command line on your local computer (not arden). If you’re a Windows user, activate WSL.
2.1. Generate a public / private key pair
To generate a public / private key pair:
- Type the following command:
ssh-keygen
- This will generate your private key inside the
.ssh
folder inside your home directory. Typically, the private key is calledid_rsa
and the public key is calledid_rsa.pub
. - Verify that this worked by typing
ls -la ~/.ssh
. You should see both files (with today’s timestamp).
2.2. Copy your public key to the appropriate place
On GitHub
- Follow the GitHub instructions
On Arden (Optional but Recommended)
- Copy your public key into your home directory on arden. One way of doing this is by using the
rsync
utility:rsync ~/.ssh/id_rsa.pub <your_arden_username>@arden.cs.unca.edu:~/.
- Configure your public key on arden:
- ssh into arden:
ssh <your_arden_username>@arden.cs.unca.edu
- Verify that the
id_rsa.pub
file is now in your home directory. - If your home directory on the remote server doesn’t already contain a
~/.ssh/authorized_keys
file, create oneas follows:mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys
- Then, append your public key to the
authorized_keys
file:cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
- Finally, delete your public key from your home directory on arden (you don’t need it anymore):
rm ~/id_rsa.pub
- ssh into arden:
That should be it! Read More here: https://kb.iu.edu/d/aews
3. Set up a local copy of the coursework repository
In this class, we’re going to have two repositories:
class-exercises-fall2023
– For in-class exercises and labsapp
– for our class project
Before we get into the details of the GitHub workflow, let’s set up a clone of class-exercises-fall2023
on your laptop while practicing some basic git commands. Please complete the following tasks:
- Within your
csci338
directory, clone theclass-exercises-fall2023
repo using the ssh method (while in thecsci338
directory on your local computer):git clone git@github.com:csci338/class-exercises-fall2023.git
- Look at commit history (
git log
) - Create a new branch called
<your-username>
- For instance, Sarah would create a branch called vanwars:
git checkout -b vanwars
- For instance, Sarah would create a branch called vanwars:
- Create a folder named
<your-github-username>
(e.g. Sarah would create a folder calledvanwars
) within theclass-exercises-fall2023
folder on your new branch. - Within the
<your-github-username>
folder, create a text file calledREADME.md
(note the case). - Within the
README.md
file, add the sentence “hello world!” (or anything, really). You can use vim, VS Code, or another text editor. - Issue the
git status
command. What happened? - Stage your changes using
git add .
(the dot indicates that you want to stage all of the files that have been added / deleted / edited). - Commit your changes using
git commit -am "adding my user directory"
. - Push your branch to GitHub (
git push
)- Note, typing
git push
will display an error with a suggested push command (e.g.,git push --set-upstream origin <your-branch-name>
).
- Note, typing
- Create a pull request.
A note on your origin path
Within git, your remote origin
variable holds both the address and the protocol you will be using to interact with a remote server (like GitHub). Some of you are accessing the remote server using the https protocol while others are using the ssh protocol. For the sake of simplicity, let’s all use ssh. To check your origin, type: git remote show origin
.
If it prints git@github.com:csci338/class-exercises-fall2023
, you don’t have to do anything. Otherwise, let’s switch up your origin protocol to ssh as follows:
1
2
3
git remote rm origin # removes current references
git remote add origin git@github.com:csci338/class-exercises-fall2023.git # adds new reference
git remote show origin # prints the new origin (which should be the correct one).
4. Set up the course project repo
4.1. Clone app
- Within your
csci338
directory, clone the course app repo via ssh:git clone git@github.com:csci338/app.git
- Be sure you don’t accidentially put
app
underneathclass-exercises-fall2023
.
- Be sure you don’t accidentially put
4.2. Make a new branch
- Create a new branch called
<your-username-readme>
- For instance, Sarah would create a branch called vanwars-readme:
git checkout -b vanwars-readme
- For instance, Sarah would create a branch called vanwars-readme:
- Open the existing
README.md
file. At the bottom, add an entry with your name and your GitHub username. Please add your information so that the table is sorted in alphabetical order by last name. add
,commit
, andpush
your changes to a remote branch of the same name.- Create a pull request
4.3. Resolve push / pull conflicts
- Once your branch is approved, check it to ensure that there are no conflicts with the
main
branch. - If there are conflicts, follow the steps below:
Workflow for resolving conflicts
1
2
3
4
5
6
7
8
9
10
11
git checkout main # checkout the main branch again
git pull # pull down the changes that have happened since you last pulled / cloned
git checkout <your-username>-readme # switch back to your branch
git rebase main # try to rebase
# Manually resolve the conflict and then continue steps below...
git status
git rebase --continue # continue the process
git push # should reject your change
git push --force # force the change
4.4. Incorporate your changes to main
When you’re done, ask Semmy or Sarah to incorporate your changes to main.
What to Turn In
Make sure that the following are completed before Wednesday (9/6) at midnight:
- You have successfully committed and pushed your username directory and
README.md
file to theclass-exercises-fall2023
repo (https://github.com/csci338/class-exercises-fall2023). - You have successfully committed and pushed your
README.md
edits to theapp
repo (https://github.com/csci338/app).