Pre-Class Assignment: Git with it! (collaborating with Git)#

Day 16#

CMSE 202#

git logo

✅ Put your name here

#

Goals for today’s pre-class assignment#

  1. Review some additional Git and GitHub functionality to increase your comfort with using Git to track your code and collaborate with others

Assignment instructions#

Watch the videos below, do the readings linked to below the videos, and complete the assigned programming problems. Please get started early, and come to office hours if you have any questions! You should also use Slack to help each other thought any issues you run into!

This assignment is due by 11:59 p.m. the day before class, and should be uploaded into the appropriate “Pre-class assignments” submission folder. Submission instructions can be found at the end of the notebook.


0. Revisiting Git#

In this pre-class assignment, we are going to revisit the versional control tool, Git. You’ve used Git a bit so far this semester mainly as a way of keep track of your own software development and solution generation. However, Git is also a powerful tool for collaboration. In this pre-class assignment, we are going to explore some of the collaborative features of Git as a way of preparing you to work in groups for your semester project.

If you’re feeling pretty rusty with Git at this point, you may find it useful to revisit the Day 3 content from earlier in the semester.

As you work through the content that follows, keep in mind that you will be expected to create and use a GitHub repository for your semester project and you should think about how you might make Git a valuable tool for collaborating with your group members.


1. Git Branches#

1.1 Why use branches?#

Imagine you’re working on a new piece of code with your friend Jacob. During the development process, your code keeps throwing errors (perfectly normal). However, your friends (and collaborators) Mika and Navi are working on a separate chunk of code at the same time. When they try to test their code, they get stuck on errors from the code that you and Jacob are developing. You need to be able to push to the repo so you and Jacob can build off of one another’s work, but working in this way is causing significant difficulties for Mika and Navi. Working on an entirely new repo isn’t an option, either, since both teams want to keep track of their past commits.

1.1.1#

What would be a solution to a problem like this?

Write your answer here.

👀 Watch the following video on branching in Git (<- click the link!)

1.1.2#

How could we use branches to solve the aforementioned problem?

Write your answer here.

1.2 Creating your first git “branch”#

Branches are a powerful and important tool when using git. However, some folks are never exposed to this extra functionalty, thus missing out on their utility and power! Once you understand how they work you will likely find ways to use them all the time (sort of like git itself).

Branches are a great location for doing new work in your git repositories. Branches are a safe place to store changes to files without fear of running into issues with work that others might be doing on the same repository. Anytime you want to change files, it is good to get into the habit of making sure you are making and committing those changes to a branch.

To create a branch just type the following:

    git branch BRANCHNAME

Once you have a new branch, you can list all of your branches (including the default “main” branch) and see which one you are on using the following command:

    git branch
    # Notice the lack of a BRANCHNAME here!

To switch between branches just use the git checkout command with the BRANCHNAME:

    git checkout BRANCHNAME

✅ Do This: Let’s test out a bit of branching. Try to go through the following steps to create a new branch, commit changes to it, and then move between that branch and the main branch:

  1. Make a new repo called day16-pre-class-repo. Clone this repo into your CMSE202/repositories folder (you should have created this space as part of the Day 3 content from a while back).

  2. In your day16-pre-class-repo folder, make sure there are no outstanding changes haven’t been committed and pushed to your repository. Double-check this with git status. There shouldn’t be any changes since you just created this repository, but if there are for some reason, commit them, and push them to your GitHub repo.

  3. Create a new branch called testing:

  4. Use git branch command to confirm that you now have this new branch in your list of all branches.

  5. “Checkout” (switch to) this new branch using the following command:

    git checkout testing
    
  6. Confirm that you are now on this new branch using git status. At the top of the status message, it should tell you which branch you are on.

  7. Once you’re in your new testing branch, create a new python script called hello_world.py that contains a simple “Hello, world!” program. Once you’ve finished your script save and close it. You should be able to do this using vim, but it might have been a while since you’ve used this. Feel free to create this “.py” file using whatever method you prefer.

  8. commit your new hello_world.py script to your testing branch and then try running git log to see that you’ve made a new commit to your new branch.

  9. If it looks like everything is good, now you’re going to try switching back to your main branch to see what happens when you do. Running the following command:

    git checkout main
    
  10. At this point, you should find that the hello_world.py file you created is gone(!) – what happened? Because that file is part of your other branch, it no longer exists when you’re working inside the “main” branch. Convince yourself that git is still safely keeping track of your file by switching back to your testing branch with:

    git checkout testing
    
  11. Confirm that your hello_world.py file as returned and breathe a deep sigh of relief!


2. Collaborating with Git/GitHub#

2.1 Working on the same code#

Now imagine that you and Jacob have finished the code you’re working on, and Mika and Navi have finished their code. You want to combine the two pieces of code into a single script, and you don’t want to do it by hand, since your code is hundreds of lines long.

2.1.1#

Is there a tool/command in Git that might help you with this process? Search around and see if you can find one.

Write your answer here.

👀 Watch the following video. (<- click the link!)

2.1.2#

How could you use the concept in the video to solve the aforementioned problem?

Write your answer here.

2.1.3#

Back in the Day 3 ICA, you were asked to share a repo with your groupmates and have everyone contribute to the same file. If you can recall how things went when you did this, did you and your group run into an issue with everyone writing to the same file? Why or why not? (If your group didn’t get to that part, try to guess whether there would have been an issue.)

Write your answer here.

✅ Do This: Since you are the main/only developer on your day16-pre-class-repo try going through the process of making changes in the main branch (which you have permission to do) and merging them into your testing branch. Essentially, try doing the following:

  1. Make sure you are on the main branch then add some new files and make sure you commit them to the repository.

  2. Once you’ve done that, see if you can use the internet to figure out how to merge changes from the main branch into the testing branch. You might also find it useful to review this tool that we introduced you to in a previous PCA: https://learngitbranching.js.org/ (if you didn’t have a chance to explore this tool previously, or you don’t remember using this tool, now would be a good time to work through the first three levels)

Do This - Erase the contents of this cell and replace it with any issues you ran into when you trying to experiment with merging files across branches.

Good work! This concludes our introduction to more of Git’s functionality. In class, you’ll have an opportunity to practice some of these things with your group!


Follow-up Questions#

Copy and paste the following questions into the appropriate box in the assignment survey include below and answer them there. (Note: You’ll have to fill out the section number and the assignment number and go to the “NEXT” section of the survey to paste in these questions.)

  1. Explain, in your own words, why “branches” in Git can be useful and give an example of how you might use this functionality when working on a coding project.

  2. Are you having any persistent issues with using Git at this time? If so, are you using the MSU JupyterHub server or are you trying to run things locally on your computer? If you are running into issues, do your best to describe the issues you’re having.


Assignment wrap-up#

Hopefully you were able to get through all of that! We’ll be trouble-shooting any issues you had, working with remote git repositories, and testing out some more complicated git commands in class.

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment!

from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://cmse.msu.edu/cmse202-pc-survey" 
	width="800px" 
	height="600px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

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

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

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