3. Publishing#
We’re still in our working directory:
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}")
git_example_dir = os.path.join(drive_dir, "learning_git", "git_example")
if os.path.exists(git_example_dir):
print(f"Git example directory: {git_example_dir}")
os.chdir(git_example_dir)
else:
print("Start from the beginning")
Not running on colab
Drive dir: /mnt/nvme1n1p2/home/yj.lee/workspace/projects/lecture/book/lectures/softeng/vcs/drive/MyDrive
Git example directory: /mnt/nvme1n1p2/home/yj.lee/workspace/projects/lecture/book/lectures/softeng/vcs/drive/MyDrive/learning_git/git_example
Creating a repository#
Ok, let’s create a repository to store our work. Hit “new repository” on the right of the github home screen, or click here.
Fill in a short name, and a description.
Choose a “public” repository.
Don’t choose to add a README.
GitHub private repositories#
For this course, you should use public repositories in your personal account for your example work: it’s good to share! GitHub is free for open source, but in general, charges a fee if you want to keep your work private.
In the future, you might want to keep your work on GitHub private.
Students can get free private repositories on GitHub, by going to GitHub Education and filling in a form (look for the Student Developer Pack).
GitHub authentication for Google Colaboratory#
To authenticate with GitHub, we need to generate a Personal Access Token (PAT).
Go to your GitHub account settings page, select “Developer settings”, then “Personal access tokens”, then “Generate new token”.
Give the token a description like “Colaboratory access” and select the scopes/permissions as needed for your usage.
Copy the token to your clipboard.
Note that this token is like a password, so keep it secret!
For the Korean explanation, see here.
Adding a new remote to your repository#
Instructions will appear, once you’ve created the repository, as to how to add this new “remote” server to your repository.
In this example we are using pre-authorised Deploy Keys
to connect using the HTTPS
protocol:
%%bash
git remote add origin https://github.com/chu-aie/github-example.git
%%bash
git remote -v
origin https://github.com/chu-aie/github-example.git (fetch)
origin https://github.com/chu-aie/github-example.git (push)
%%bash
git push -uf origin main # Note we use the '-f' flag here to force an update
To https://github.com/chu-aie/github-example.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
Remotes#
The first command sets up the server as a new remote
, called origin
.
Git, unlike some earlier version control systems is a “distributed” version control system, which means you can work with multiple remote servers.
Usually, commands that work with remotes allow you to specify the remote to use, but assume the origin
remote if you don’t.
Here, git push
will push your whole history onto the server, and now you’ll be able to see it on the internet! Refresh your web browser where the instructions were, and you’ll see your repository!
Let’s add these commands to our diagram:
Playing with GitHub#
Take a few moments to click around and work your way through the GitHub interface. Try clicking on ‘test.md’ to see the content of the file: notice how the markdown renders prettily.
Click on “commits” near the top of the screen, to see all the changes you’ve made. Click on the commit number next to the right of a change, to see what changes it includes: removals are shown in red, and additions in green.
Working with multiple files#
Some new content#
So far, we’ve only worked with one file. Let’s add another:
vim lakeland.md
%%writefile lakeland.md
Lakeland
========
Cumbria has some pretty hills, and lakes too.
Writing lakeland.md
!cat lakeland.md
Lakeland
========
Cumbria has some pretty hills, and lakes too.
Git will not by default commit your new file#
%%bash
git commit -am "Try to add Lakeland" || echo "Commit failed"
On branch main
Your branch is up to date with 'origin/main'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
lakeland.md
nothing added to commit but untracked files present (use "git add" to track)
Commit failed
This failed, because we’ve not told git to track the new file yet.
Tell git about the new file#
%%bash
git add lakeland.md
git commit -am "Add lakeland"
[main b943bed] Add lakeland
1 file changed, 4 insertions(+)
create mode 100644 lakeland.md
Ok, now we have added the change about Cumbria to the file. Let’s publish it to the origin repository.
%%bash
git push
To https://github.com/chu-aie/github-example.git
9793609..b943bed main -> main
Visit GitHub, and notice this change is on your repository on the server. We could have said git push origin
to specify the remote to use, but origin is the default.
Changing two files at once#
What if we change both files?
%%writefile lakeland.md
Lakeland
========
Cumbria has some pretty hills, and lakes too
Mountains:
* Helvellyn
Overwriting lakeland.md
%%writefile test.md
Mountains and Lakes in the UK
===================
Engerland is not very mountainous.
But has some tall hills, and maybe a
mountain or two depending on your definition.
Overwriting test.md
%%bash
git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: lakeland.md
modified: test.md
no changes added to commit (use "git add" and/or "git commit -a")
These changes should really be separate commits. We can do this with careful use of git add, to stage first one commit, then the other.
%%bash
git add test.md
git commit -m "Include lakes in the scope"
[main d2b4434] Include lakes in the scope
1 file changed, 4 insertions(+), 3 deletions(-)
Because we “staged” only test.md, the changes to lakeland.md were not included in that commit.
%%bash
git commit -am "Add Helvellyn"
[main 4fa39c8] Add Helvellyn
1 file changed, 4 insertions(+), 1 deletion(-)
%%bash
git log --oneline
4fa39c8 Add Helvellyn
d2b4434 Include lakes in the scope
b943bed Add lakeland
9793609 Revert "Add a lie about a mountain"
6626d56 Change title
453f5bc Add a lie about a mountain
74efc42 First commit of discourse on UK topography
%%bash
git push
To https://github.com/chu-aie/github-example.git
b943bed..4fa39c8 main -> main