Python Worksheet 2: Git

29 Aug 2023, by Aadi

Back to all sheets

We’re going to take an brief and unexpected turn towards git for this sheet.

Just like better brakes are what allow a car to go faster, source control software like Git is what allows for rapid experimentation, learning, discovery, and of course, version control itself.

When writing programmes, we deal with a lot of text files. These files are never done. They keep evolving – we add features, or fix bugs, or make the code look better, or just work better. Sometimes many people are working on different parts of the same file. git is a programme made for storing versions of, and keeping track of text files as they evolve. Although it is designed with source code, it can be used for any text files.

You’re now hopefully convinced that you should be learning git.

One concise resource I found on the internet is GitHub’s cheatsheet.

Checking if git is installed.

  1. Try running
git --version

in the Terminal. This should tell you the version. As of August 2023, version 2.42.0 is the latest.

Linux or macOS should have git already installed. Check this link for instructions if necessary.

Create a new repository!

  1. With the Terminal or otherwise, create a new folder.
    This folder is going to hold all the Python programmes from this course, so choose a name and location suitably.
    Then, cd into the folder.
    From within this folder, run
git init

This will make this folder into a repository.
Verify that you’ve done this correctly by running

git status

Read the printed message carefully.

Adding files

  1. If you’ve completed sheet 1, then you must be having a couple of python files. Why not add them to our new repo?
    So copy or move them over into this new folder.
    Then run git status in the same place as where you’ve run all your previous Git commands. What do you see?
    Now Git does not say “Nothing to commit”, and instead lists your files as untracked.
    In fact, if you look closely, it also tells us what to do!
    We need to run
git add file.py

for each file.py that you wish to track.
This command tells git to record this version of this file.
Verify from git status that each of these added files are staged for commit.

add is used for tracking untracked files, and also for saving new versions of already-tracked files. add is basically save this version of this particular file while commit is save this version of the whole repository.

Making your first commit

  1. A commit is a record of the current state of your repository. (Although that’s not the only way of thinking about a commit, it’s good enough to begin with.)
    There’s plenty of information stored in a commit. A commit has a parent, which tells us which commit preceded it. Sometimes it might have two parents, if it is, for example, a merge commit. A commit also has an author (describing the person that created the commit), and a commit message (describing what the commit contains.) The command
git commit

will let you start making the commit.

At this point, there’s a good chance that Git complains to you about names and email addresses. That’s because Git records a name and email address with each commit, and if it’s the first time you’re running it, it doesn’t have any names and addressed stored. So follow the instructions that it gives (you may also check the Configure tooling section of the cheatsheet above) to provide Git with a name and email address. We are going to be making this repo publicly available, so if you’re not okay with making that information public, it’s okay to enter something else. It doesn’t matter.

Every time you commit, Git will ask for a commit message by opening a text editor for you to type in. Enter the message (usually describing the change from the last commit eg. “fixed bug in isEven”. For the first commit, you can just put “Initial commit”.)

Save the file and exit the text editor (Search the web if you’re not sure how to do that.)

Now verify that your commit has been created by running

git log

Press q to exit the log.


That’s it for now!

You know know how the 3 basic commands of Git: status, add, and commit.

Every time you change anything in the repository, run git status to see all the files that have changed. Then git add all the files you wish to save versions of (recall that add works for tracked as well as untracked files). Once you have added all the files, run git commit just like before.

Get into a habit of using Git! It’s going to help you later!

Bonus!

If this sheet went by too fast, here’s an extra question.
Create an account on GitHub. Make yourself comfortable with GitHub, follow their tutorial and whatnot. Then upload this repository there. Make sure it has a file called README.md so that anybody visiting your repo knows what it’s about!