0. Introduction to version control#
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)#
Sue |
James |
---|---|
|
… |
… |
Join the team |
… |
|
… |
Do some programming |
… |
|
|
… |
Do some programming |
Do some programming |
|
… |
|
… |
|
… |
|
… |
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
).
## Git Exercise
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.
Markdown#
The text files we create will use a simple “wiki” markup style called markdown to show formatting. This is the convention used in this file, too.
You can view the content of this file in the way Markdown renders it by looking on the web, and compare the raw text.
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.
Commands you can type will look like this, using the %%bash “magic” for the notebook.
If you are running the notebook on windows you’ll have to use %%cmd.
%%bash
echo some output
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
Writing somefile.md
But if you are following along, you should edit the file using a text editor.
Setting up somewhere to work#
import os
try:
from google.colab import drive # type: ignore
drive.mount("/content/drive")
drive_dir = "/content/drive/MyDrive"
except ImportError:
print("Not running on colab")
drive_dir = os.path.join(os.getcwd(), "drive", "MyDrive")
os.makedirs(drive_dir, exist_ok=True)
print(f"Drive dir: {drive_dir}")
Not running on colab
Drive dir: /mnt/nvme1n1p2/home/yj.lee/workspace/projects/lecture/book/lectures/softeng/vcs/drive/MyDrive
git_example_dir = os.path.join(drive_dir, "learning_git", "git_example")
if os.path.exists(git_example_dir):
import shutil
print("Removing existing git_example directory")
shutil.rmtree(git_example_dir)
else:
print("Creating new git_example directory")
os.makedirs(git_example_dir, exist_ok=True)
print(f"Git example directory: {git_example_dir}")
Creating new git_example directory
Git example directory: /mnt/nvme1n1p2/home/yj.lee/workspace/projects/lecture/book/lectures/softeng/vcs/drive/MyDrive/learning_git/git_example
We need to move this Jupyter notebook’s current directory as well:
os.chdir(git_example_dir)
!pwd
/mnt/nvme1n1p2/home/yj.lee/workspace/projects/lecture/book/lectures/softeng/vcs/drive/MyDrive/learning_git/git_example
Solo work#
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 --initial-branch=main
/raid/cis/yjlee/workspace/projects/lecture/book/lectures/softeng/vcs/learning_git/git_example/learning_git/git_example
Initialized empty Git repository in /raid/cis/yjlee/workspace/projects/lecture/book/lectures/softeng/vcs/learning_git/git_example/learning_git/git_example/.git/
As yet, this repository contains no files:
%%bash
ls
%%bash
git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Configuring Git with your name and email#
First, we should configure Git to know our name and email address:
git config user.name "YOUR NAME HERE"
git config user.email "yourname@example.com"
Use the --global
flag, if you want to use the same name and email address for all your Git repositories.
%%bash
git config user.name "YOUR NAME HERE"
git config user.email "yourname@example.com"
Now check that this worked
%%bash
git config --get user.name
Young Joon Lee
%%bash
git config --get user.email
entelecheia@hotmail.com