Version control and code review

Objectives

  • Browse commits and branches of a Git repository.

  • Remember that commits are like snapshots of the repository at a certain point in time.

  • Know the difference between Git (something that tracks changes) and GitHub/GitLab (a platform to host Git repositories).

Why do we need to keep track of versions?

Version control is an answer to the following questions (do you recognize some of them?):

  • “It broke … hopefully I have a working version somewhere?”

  • “Can you please send me the latest version?”

  • “Where is the latest version?”

  • “Which version are you using?”

  • “Which version have the authors used in the paper I am trying to reproduce?”

  • “Found a bug! Since when was it there?”

  • “I am sure it used to work. When did it change?”

  • “My laptop is gone. Is my thesis now gone?”

Demonstration

  • Example repository: https://github.com/coderefinery/planets

  • Commits are like snapshots and if we break something we can go back to a previous snapshot.

  • Commits carry metadata about changes: author, date, commit message, and a checksum.

  • Branches are like parallel universes where you can experiment with changes without affecting the default branch: https://github.com/coderefinery/planets/network (“Insights” -> “Network”)

  • With version control we can annotate code (example).

  • Collaboration: We can fork (make a copy on GitHub), clone (make a copy to our computer), review, compare, share, and discuss.

  • Code review: Others can suggest changes using pull requests or merge requests. These can be reviewed and discussed before they are merged. Conceptually, they are similar to “suggesting changes” in Google Docs.

Where to explore more

Exercises

Exercise Git-1: Turn your project to a Git repo and share it

  1. Create a new directory called myproject with one or few files in it. This represents our own project. It is not yet a Git repository. You can try that with your own project or use a simple placeholder example.

  2. Turn this new directory into a Git repository.

  3. Share this repository on GitHub (or GitLab, since it really works the same).

We offer three different paths of how to do this exercise.

  • Via GitHub web interface: easy and can be a good starting point if you are completely new to Git.

  • VS Code is quite easy, since VS Code can offer to create the GitHub repositories for you.

  • Command line: you need to create the repository on GitHub and link it yourself.

Create an repository on GitHub

First log into GitHub, then follow the screenshots and descriptions below.

Screenshot on GitHub before a new repository form is opened

Click on the “plus” symbol on top right, then on “New repository”.

Then:

Screenshot on GitHub just before a new repository is created

Choose a repository name, add a short description, and in this case make sure to check “Add a README file”. Finally “Create repository”.

Upload your files

Now that the repository is created, you can upload your files:

Screenshot on GitHub just before uploading files

Click on the “+” symbol and then on “Upload files”.

Exercise Git-2: Create a new branch and a new commit

  • Fork the example repository: https://github.com/coderefinery/planets

  • Create a new branch in your fork and give it a descriptive name.

  • Make a modification on the new branch and create a new commit.

  • The new branch and the new commit now only exist on the fork, not yet in the original repository.

  • In case you would like to contribute your change back to the original repository, you would create a pull request (you are welcome to try).

Exercise Git-3: Archaeology using Git annotate (“blame”)

Your goal is to find out when precisely this line was modified last time (which commit)?