Python Worksheet 2: Git
29 Aug 2023, by
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
One concise resource I found on the internet is GitHub’s cheatsheet.
git is installed.
- Try running
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!
- 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.
cdinto the folder.
From within this folder, run
This will make this folder into a repository.
Verify that you’ve done this correctly by running
Read the printed message carefully.
- If you’ve completed sheet 1, then you must be having a couple of python
Why not add them to our new repo?
So copy or move them over into this new folder.
git statusin 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
file.py that you wish to track.
This command tells
git to record this version of this file.
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
- 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
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
q to exit the log.
That’s it for now!
You know know how the 3 basic commands of Git:
Every time you change anything in the repository,
git status to see all the files that have changed.
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!
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!