Fork me on GitHub

Introduction to version control with Git

This is the introductory lesson to version control using Git. It is assumed to be the very first thing done in a course.

Our philosophy is that we start from own local repository, branching and merging (locally), and a brief introduction to pushing to remotes. In the separate collaborative git lesson, we teach more use of remote repositories and good collaborative workflows. We try to stick to simple workflows, just enough for researchers who aren’t obsessed with git to be able to work well. We try to avoid commands which might get you into a confusing state.

Our general flow is 10 minutes on “why version control”, followed by about 45 minutes of the basics of how to make commits. Then, there are several small steps until we get to branching, merging, and conflict resolution.

Investigating a past commit and going back in time are also introduced. Great emphasis is made about the importance of not changing history of shared repositories, when there is a need to undo commits.

Then, we see a little bit about sharing repositories online, how to effectively dig into the history of a project, and a few other random points before we conclude.

The goals of the module as a whole are that the user will feel comfortable about staging changes, committing them, merging, and branching. The guacamole example that we use is inspired by Byron Smith, for original reference, see this thread. If you are teaching this lesson, see the instructor’s guide

Prerequisites

  • A reasonably recent version of Git is installed. For installation instructions and binaries see the Git home page.
  • Being comfortable with the command line. No expertise is required, but the lesson will be mostly taken from the command line.
  • Students should be familiar with using a text editor on their system. Emacs and Vim are excellent choices if you know how to use them but Nano or Notepad on Windows are sufficient.
  • Git should be configured prior to the lesson following our installation instructions.
  • A GitHub user account (but alternatives exist, see below).

Why GitHub?

In this introduction we will mention and use GitHub but also GitLab and Bitbucket allow similar workflows and basically everything that we will discuss is transferable. With this material and these exercises we do not endorse the company GitHub. We have chosen to demonstrate a number of concepts using examples with GitHub because it is currently the most popular web platform for hosting Git repositories and the chance is high that you will interact with GitHub-based repositories even if you choose to host your Git repository on another platform.

Course participants are most welcome to use our new Nordic research software repository platform instead, for more information see https://coderefinery.org/repository/.

If you are interested in the Git-aware prompt you can find it on GitHub.

Schedule

15 min Motivation Why version control?
Why Git?
35 min Basics What is Git?
What is a repository?
How does Git operate?
How do I make commits?
How do I select what to commit?
20 min Using the Git staging area Why do we recommend to first add, then commit a change?
What should be included in a single commit?
20 min Undoing things How can I undo things?
35 min Branching and merging How can I or my team work on multiple features in parallel?
How to combine the changes of parallel tracks of work?
How can I permanently reference a point in history, like a software version?
30 min Conflict resolution How can we resolve conflicts?
How can we avoid conflicts?
20 min Sharing repositories online How can I set up a public repository online?
How can I clone a public repository to my computer?
How does version control scale from 1 to N users per repository?
35 min Inspecting history How can we find out when exactly a line of code was changed?
How can we find out which commit broke or changed a functionality?
15 min (Optional) Interrupted work How can Git help us to deal with interrupted work and context switching?
10 min (Optional) Aliases and configuration How can I remember all these Git command options?
Is there too much configuration?
20 min (Optional) Git under the hood Where are commits stored?
What are branches really?
10 min (Optional) Practical advice: how much git is necessary? What level of complexity is necessary for each project?
Is one branch enough?