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-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)?
Solution
It was this commit: https://github.com/coderefinery/planets/commit/56cf6fdfef6a516ee369034d7c67a20237abb368