Introduction¶
What's version control?¶
Version control is a tool for managing changes to a set of files.
There are many different version control systems:
- Git
- Mercurial (
hg
) - CVS
- Subversion (
svn
) - ...
Why use version control?¶
- Better kind of backup.
- Review history ("When did I introduce this bug?").
- Restore older code versions.
- Ability to undo mistakes.
- Maintain several versions of the code at a time.
Git is also a collaborative tool:
- "How can I share my code?"
- "How can I submit a change to someone else's code?"
- "How can I merge my work with Sue's?"
Git != GitHub¶
Git: version control system tool to manage source code history.
GitHub: hosting service for Git repositories.
How do we use version control?¶
Do some programming, then commit our work:
my_vcs commit
Program some more.
Spot a mistake:
my_vcs rollback
Mistake is undone.
What is version control? (Team version)¶
Graham | Eric |
---|---|
my_vcs commit |
... |
... | Join the team |
... | my_vcs checkout |
... | Do some programming |
... | my_vcs commit |
my_vcs update |
... |
Do some programming | Do some programming |
my_vcs commit |
... |
my_vcs update |
... |
my_vcs merge |
... |
my_vcs commit |
... |
Scope¶
This course will use the git
version control system, but much of what you learn will be valid with other version control
tools you may encounter, including subversion (svn
) and mercurial (hg
).
Practising with Git¶
Example Exercise¶
In this course, we will use, as an example, the development of a few text files containing a description of a topic of your choice.
This could be your research, a hobby, or something else. In the end, we will show you how to display the content of these files as a very simple website.
Programming and documents¶
The purpose of this exercise is to learn how to use Git to manage program code you write, not simple text website content, but we'll just use these text files instead of code for now, so as not to confuse matters with trying to learn version control while thinking about programming too.
In later parts of the course, you will use the version control tools you learn today with actual Python code.
Displaying Text in this Tutorial¶
This tutorial is based on use of the Git command line. So you'll be typing commands in the shell.
To make it easy for me to edit, I've built it using Jupyter notebook.
Commands you can type will look like this, using the %%bash "magic" for the notebook.
NOTE: using bash/git commands is not fully supported on jupyterlite yet (due to single thread/process restriction), and the cells below might error out on the browser (jupyterlite) version of this notebook
%%bash
echo some output
with the results you should see below.
In this document, we will show the new content of an edited document like this:
%%writefile somefile.md
Some content here
But if you are following along, you should edit the file using a text editor. On either Windows, Mac or Linux, we recommend VS Code.
Setting up somewhere to work¶
%%bash
rm -rf learning_git/git_example # Just in case it's left over from a previous class; you won't need this
mkdir -p learning_git/git_example
cd learning_git/git_example
I just need to move this Jupyter notebook's current directory as well:
import os
top_dir = os.getcwd()
top_dir
git_dir = os.path.join(top_dir, 'learning_git')
git_dir
working_dir=os.path.join(git_dir, 'git_example')
os.chdir(working_dir)
%%bash
git config --global user.name "Lancelot the Brave"
git config --global user.email "l.brave@spamalot.uk"
Additionally, it's also a good idea to define what's the name of the default branch when we create a repository:
%%bash
git config --global init.defaultBranch main
Historically, the default branch was named master
. Nowadays, the community and most of the hosting sites have changed the default (read about this change in GitHub and Gitlab.
Initialising the repository¶
Now, we will tell Git to track the content of this folder as a git "repository".
%%bash
pwd # Note where we are standing-- MAKE SURE YOU INITIALISE THE RIGHT FOLDER
git init
As yet, this repository contains no files:
%%bash
ls
%%bash
git status