In-Class Assignment: Practice with Git and GitHub#

Day 3#

CMSE 202#

✅ Put your name here

#

✅ Put your group member names here

#

Learning goals for today#

By the end of class you should:

  1. Be a bit more comfortable using git on the commandline so that you can clone and modify repositories

  2. Be able to create a repository on GitHub, clone it to your computer, and add, modify, and push changes to the repository. You should also be able to pull changes made by someone else.

  3. Understand how to give other people access to your GitHub repositories so that they can make changes as well.

Agenda for today:#

  1. Review of pre-class assignment: thinking about version control and git

  2. Reference guide for git commands

  3. Practice downloading and exploring examples using git

  4. Let’s “Git” practicing: setting up and managing a new GitHub repository

Assignment instructions#

Work through the notebook with your group. Making sure to write all necessary code and answer any questions. Do your best to finish the assignment, but it’s OK if you don’t make it to the end.

This assignment is due by the end of class, and should be uploaded into the appropriate “In-Class assignments” submission folder. Submission instructions can be found at the end of the notebook.


1. Review of pre-class assignment: thinking about version control and git#

Did anyone have any specific issues with the pre-class assignment?

Let’s take a moment to highlight some key concepts. Discuss with your table the following prompts and write down a brief definition of each of these concepts.

If you don’t feel like you have good working definitions yet, try doing a quick internet search to see if you can find a definition that makes sense to you.

✅ 1.1: Discuss what we are referring to when we talk about a code “repository”.

Do This - Write your disussion notes here.

✅ 1.2: Discuss what we mean when we talk about “version history” vs “version control”

Do This - Write your disussion notes here.

✅ 1.3: Discuss why having a version control tool like git can be useful

Do This - Write your disussion notes here.

✅ 1.4: Discuss the purpose/utility of the git clone command

Do This - Write your disussion notes here.


2. Reference guide for git commands#

In class we will be exploring git and some of the most common commands. Note there are a lot of other commands for using git as it is a very powerful tool. However, we can’t cover everything so this list just provides the most basic commands that let you create and contribute to a repository.

Checking out a git repository#

You’ve reviewed this one and the pre-class assignment and talked about it with your group. You need a URL for the repository. You also need to have permissions to access the repository. If the repository is public there is no problem. If the repository is private you will need a password, “personal access token”, or may need to set up an ssh key, which we may talk about at a later time.

  • git clone URL - Check out initial git repository

Creating a new git repository#

Creating a local/empty repository is easy. You can just use the “git init” command. Just make sure you run the command in the directory that you want to turn into a repository.

  • git init . - Create a new/empty repository. (the “.” is important!)

This only creates a local git repository. To sync this repository with a server (GitLab or GitHub) you need to create a “new project” on the website and follow the provided directions for syncing your local repository with the remote one.

Pulling in changes from the server.#

You got a glimpsed of this in the pre-class assignment and we’ll practice using it today. You can just run the “git pull” command to pull from the default repository:

  • git pull - Download changes from the central repository.

Or, if you want to pull from a different repository, you can specific the repository path:

  • git pull < path to remote repository > - Download changes from another repository.

NOTE: git pull can be tricky! If you have modified any files and not committed them, those will be changed by the git pull. We’ll learn more information about merging in git later on.

Adding and Commiting a change#

The most common activity on a git repository is commiting a change. Typically you are either adding a new file or changing an existing file. The steps I almost always use are as follows:

  • git status - check to see what files are different

    • Existing files that have been changed will be listed under “Changes not staged for commit:”

    • New files that have not yet been added will be listed under “Untracked files:”

  • git add filename - Add new file(s) or changed file(s) to the current commit.

  • git status - check to make sure the files are included in the next commit.

    • These files will be listed under “Changes to be committed:”

  • git commit -m “Message about the changes” - Commit the changes to the repository while providing a description of the changes using the -m flag. You are now tracking the changes locally.

  • git commit -a -m Message about the changes” - Add modified files for staging with the -a flag and commit them with a message (-m) all in one command.

  • git push - Push all committed changes the changes to the server.

Some guidelines:

  • Commit early and often.

  • Do not commit binary files (unless you really mean it)

  • To not “push” any changes that break something that was working.


3. Practice downloading and exploring examples using git#

For this section, you’re going to practice cloning a variety of repositories from varying locations and use some of your new command line and git skills to poke around and see what the repositories contain. When you run into issues, reach out to your instructors and talk with your group.

3.1 Clone repositories from Github#

The GitHub website that has massive collection of publicly availabe code and Jupyter-based examples. We are going to download a couple of examples, see if we can get them working, and explore them a bit with our new git skills:

Setting up a GitHub account#

If you haven’t already, you need to set up an account on GitHub. Go to and sign up for an account using your “@msu.edu” email address. For consistency, it is also useful if you make your username match your MSU NetID.

Note: After creating this account and when you start using git, you may notice that you will need to log into your github account. There should be prompts that come up when first trying to use git. However, if they do not pop-up, you will may need to add your account information manually. You can do this by the commands found below

git config --global user.name "Your name here"
git config --global user.email "your_email@example.com"

Hopefully you’be got everything set up on Github, let’s dive in!

🛑 STOP#

check

✅ 3.1.1 Do This Make a directory called “repositories” in your “CMSE202” folder (if you haven’t already done so). Go into the “repositories” directory.

✅ Then Do This Now run the following command:

git clone https://github.com/fperez/science-journal-demo

✅ 3.1.2: Take a second and look through this git repository. What does this repo do? Discuss with your groupmates.

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

✅ 3.1.3: As a group, brainstorm things you could do with the tools provided in the “science-journal” demo.

Do This - Erase the contents of this cell and replace it with some of the ideas you brainstormed as a bulleted list.

✅ 3.1.4: Try using git log to explore the version history of this repository (it’s pretty short). What information does git log provide you with?

Do This - Erase the contents of this cell and replace it with your answer to the above question!

3.2 Finding and working with repos on your own#

✅ 3.2.1 - Find and download the Neural-Networks-Demystified git repository into your repositories directory. What command did you use to download the repository?

Do This - Erase the contents of this cell and replace it with your answer to the above question! Try using the Markdown formatting tricks to make the formatting match the commands shown above.

✅ 3.2.2: What is the purpose of this repository? Again, discuss with your group.

Do This - Erase the contents of this cell and replace it with your answer to the above question!

✅ 3.2.3: Who is the author of this repository and where are they located? How did you figure this out?

Do This - Erase the contents of this cell and replace it with your answer to the above question!

✅ 3.2.4: Use git log again to determine when this repository was last updated.

Do This - Erase the contents of this cell and replace it with your answer to the above question!

Reflecting on accessing publicly available git repositories#

As you move forward through the course, remember that the internet has a wealth of information and interesting code. When you embark on a new project, it can be worth hunting around to see if someone has some something similar that you can leverage to accomplish your goals. However, any time you make use of someone else’s publicly available code, make sure to cite them in your code and link back to the original source! Failing to give credit where credit is due is not only considered plagiarism but goes against the ideal ethics for a productive open-source code community.


4. Let’s “Git” practicing: setting up and managing a new GitHub repository#

To start out on this activity, you’ll do a bit of work individually to set up, clone, modify, and push to a GitHub-hosted git repository that you are the owner of. By going through this process, you’ll be continuing to play around with Git and to get some practice for when you need to interact with GitHub repositories in the future, including for your group project this semester!

4.1 Building a testing repo#

4.1.1: Go to https://github.com/ individually create a new private git repository. Note that all GitHub repositories will default to being public, but for right now we want to create a sandbox for you to play in that isn’t shared to the whole world :)

Call this repository USERNAME_git_testing where USERNAME is replaced by your MSU NetID (the part of your email address before the “@”).

You should also initialize your repository with a “README” file - you’ll be editing that momentarily.

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.

4.1.2: Clone this new repository into your ~/CMSE202/repositories directory.

MAKE SURE YOU HAVE YOUR PERSONAL ACCESS TOKEN.#

Information on how to get your PAT can be found in the pre-class assignment (there is a video that guides you through the process).

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.

4.1.3: Once you’ve cloned the repository, open the “README.md” file that should have been created inside of the repository folder when you set it up on GitHub. Add the following information to the README file:

  • Name

  • Email Address

  • What is your major?

  • What year are you in school?

  • Where are you from?

  • What is your programming background?

Note: The README is a Markdown file, so the same techniques for formatting the text that work in Jupyter notebooks will also work in this file, but you won’t see the formatting until you push it back to GitHub.

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.

4.1.4: commit these changes to your repository. Check your commits with git log. Finally, push the changes back to the central repository on GitHub.

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.

4.1.5: Go check the GitHub website for your new repository and confirm that the changes to the README file show up – they should be on the homepage for your repository now.

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.

4.2 Sharing repos#

4.2.1: Create a new file in your repository and call it group_information.md. To start the file, add a Markdown heading at the top like so:

# Group member names and information

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.

4.2.2: Add and commit this new file to your repository and push it back up to GitHub (confirm that it shows up on the web once you’ve done this).

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.

4.2.3: Share your GitHub username with your group members and ask your group members for their usernames as well. Once you have their usernames, you need to add them as collaborators on your new repository. (If you’re unsure of how to do this, try googling it!)

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.

4.2.4: Share a direct link to your GitHub repository so that your group mates can access it easily. You can share his information by starting a group chat on Slack. Then, ask your group members to try cloning your repository.

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.

4.2.5 Every group member should add their name and MSU email address to the group_information.md file. Save the file and then commit your changes.

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.

4.2.6: Try pushing the changes you’ve made to your group members’ repositories so that they get your contact information.

NOTE: You might get a message from git that your attempt to push changes has been rejected. This tends to happen if someone else pushes changes before you did. In a first attempt to resolve this, you should do a git pull to see if you can successfully merge their changes with your own. If this is successful, you should then be able to push your changes. Sometimes, however, you’ll get a “merge conflict” which is an indication that your changes are crashing into someone else changes. This is a bit of a teaser for next class; for now, try your best to make sense of what a merge conflict is and talk things through with your group, ask instructors for help, and consult the internet.

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.

4.2.7: Do a git pull for your original repository so that you have the contact information from your group members. If done correctly, eventually each repository will have the names and emails for all of the team members in the group_information.md file. Be patient at this point, getting used to working collaborative on GitHub repositories takes time!

Note any problems you had with the above step (or any questions) here. Check in with your group to see if they can help troubleshoot any issues you run into.


Wrapping up#

Once everyone has managed to set up their own repository and has successfully added their group members as collaborators, you’re done with this activity! If you have extra time, feel free to explore the GitHub website a bit more and see what other features it has to offer. If you have questions about anything that came up during this activity, make sure to ask your instructors about them!


Congratulations, you’re done with your in-class assignment!#

Now, you just need to submit this assignment by uploading it to the course Desire2Learn web page for today’s in-class assignment submission folder. (Don’t forget to add all of the appropriate names in the first cell).

© Copyright 2024, Department of Computational Mathematics, Science and Engineering at Michigan State University